千家信息网

微信小程序制作音乐播放器的代码怎么写

发表于:2025-02-21 作者:千家信息网编辑
千家信息网最后更新 2025年02月21日,这篇文章主要介绍了微信小程序制作音乐播放器的代码怎么写的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇微信小程序制作音乐播放器的代码怎么写文章都会有所收获,下面我们一起来看
千家信息网最后更新 2025年02月21日微信小程序制作音乐播放器的代码怎么写

这篇文章主要介绍了微信小程序制作音乐播放器的代码怎么写的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇微信小程序制作音乐播放器的代码怎么写文章都会有所收获,下面我们一起来看看吧。

  网络请求的函数为:

  1. function getAlbumInfo(id,callback){

  2. var data = {

  3. albummid: albummid,

  4. g_tk: 5381,

  5. uin: 0,

  6. format: 'json',

  7. inCharset: 'utf-8',

  8. outCharset: 'utf-8',

  9. notice: 0,

  10. platform: 'h6',

  11. needNewCode: 1,

  12. _: Date.now()

  13. };

  14. wx.request({

  15. url: 'http://c.y.qq.com/v8/fcg-bin/fcg_v8_album_info_cp.fcg',

  16. data: data,

  17. header: {

  18. 'Content-Type': 'application/json'

  19. },

  20. success: function (res) {

  21. console.log(res);

  22. if (res.statusCode == 200) {

  23. callback(res.data);

  24. } else {

  25. }

  26. }

  27. });

  28. }

  29. module.exports = {

  30. ...

  31. getAlbumInfo:getAlbumInfo

  32. }

复制代码

  页面的布局代码为:

  1. {{albumInfo.name}}

  2. {{albumInfo.singername}}

  3. {{albumInfo.aDate}}

  4. {{albumInfo.genre}}

  5. {{index+1}}

  6. {{item.songname}}

  7. |

  8. {{item.name}}

  9. 简介

  10. {{albumInfo.desc}}

复制代码

  简介部分的格式文件:

  1. .desc {

  2. box-sizing: border-box;

  3. font-size: 14px;

  4. padding: 40px 10px;

  5. color: #fff;

  6. line-height: 20px;

  7. }

  8. .desc-title {

  9. text-align: center;

  10. width: 100%;

  11. font-size: 16px;

  12. margin-bottom: 20px;

  13. }

复制代码

  加载数据的代码为:

  1. var MusicService = require('../../services/music');

  2. var app = getApp()

  3. Page({

  4. data: {

  5. albumInfo: {},

  6. coverImg: '',

  7. },

  8. onLoad: function (options) {

  9. // 页面初始化 options为页面跳转所带来的参数

  10. var mid = app.globalData.zhidaAlbummid;

  11. MusicService.getAlbumInfo(mid, this.setPageData)

  12. },

  13. setPageData: function (data) {

  14. if (data.code == 0) {

  15. var albummid = data.data.mid;

  16. var img = 'http://y.gtimg.cn/music/photo/mid_album_500/' + albummid.slice(-2, -1) + '/' + albummid.slice(-1) + '/' + albummid + '.jpg'

  17. this.setData({albumInfo: data.data, coverImg: img});

  18. }

  19. },

  20. })

复制代码

  这里的点击事件与前文相同,就不再重复了。

  另外,我们在首页里未完成的两个点击事件,现在也可以完成了。先看电台的点击事件,这个事件与我们刚刚完成的一样,具体代码为:

  1. radioTap: function (e) {

  2. var dataSet = e.currentTarget.dataset;

  3. MusicService.getRadioMusicList(dataSet.id, function (data) {

  4. wx.navigateTo({

  5. url: '../play/play'

  6. });

  7. if (data.code == 0) {

  8. var list = [];

  9. var dataList = data.data;

  10. for (var i = 0; i < dataList.length; i++) {

  11. var song = {};

  12. var item = dataList[i];

  13. song.id = item.id;

  14. song.mid = item.mid;

  15. song.name = item.name;

  16. song.title = item.title;

  17. song.subTitle = item.subtitle;

  18. song.singer = item.singer;

  19. song.album = item.album

  20. song.img = 'http://y.gtimg.cn/music/photo_new/T002R150x150M000' + item.album.mid + '.jpg?max_age=2592000'

  21. list.push(song);

  22. }

  23. app.setGlobalData({

  24. playList: list,

  25. playIndex: 0

  26. });

  27. }

  28. });

  29. },

复制代码

  这里面getRadioMusicList为网络请求,具体代码为:

  1. function getRadioMusicList(id,callback){

  2. var data = {

  3. labelid: id,

  4. g_tk: 5381,

  5. uin: 0,

  6. format: 'json',

  7. inCharset: 'utf-8',

  8. outCharset: 'utf-8',

  9. notice: 0,

  10. &nnbsp; platform: 'h6',

  11. needNewCode: 1,

  12. _: Date.now(),

  13. }

  14. wx.request({

  15. url: 'https://c.y.qq.com/v8/fcg-bin/fcg_v8_radiosonglist.fcg',

  16. data: data,

  17. header: {

  18. 'Content-Type': 'application/json'

  19. },

  20. success: function (res) {

  21. if (res.statusCode == 200) {

  22. callback(res.data);

  23. } else {

  24. }

  25. }

  26. });

  27. }

  28. module.exports = {

  29. ...

  30. getRadioMusicList:getRadioMusicList

  31. }

复制代码

  另一部分为搜索结果里歌曲的点击事件

  1. musuicPlay: function (e) {

  2. var dataSet = e.currentTarget.dataset;

  3. var playingSongs = app.globalData.playList;

  4. if (typeof dataSet.index !== 'undefined') {

  5. var index = dataSet.index;

  6. var item = this.data.searchSongs[index];

  7. var song = {};

  8. var album = {};

  9. album.mid = item.albummid

  10. album.id = item.albumid

  11. album.name = item.albumname;

  12. album.desc = item.albumdesc

  13. song.id = item.songid;

  14. song.mid = item.songmid;

  15. song.name = item.songname;

  16. song.title = item.songorig;

  17. song.subTitle = '';

  18. song.singer = item.singer;

  19. song.album = album;

  20. song.time_public = item.time_public;

  21. song.img = 'http://y.gtimg.cn/music/photo_new/T002R150x150M000' + album.mid + '.jpg?max_age=2592000'

  22. this.addPlayingSongs(song);

  23. }

  24. },

复制代码

  前面的内容与我们写过的一样,最后我们没有直接更新全局变量而是调用了一个新方法,因为前文所有的点击事件都更新了整个播放列表,而我们点击某一首歌曲时,我们希望添加这首歌到已有的列表中,而不是先清空它。

  1. addPlayingSongs: function (song) {

  2. var playingSongs = app.globalData.playList; //获取当前的播放列表

  3. var index = -1;

  4. if (typeof playingSongs === 'undefined') { //判断列表是否为空

  5. playingSongs = [];

  6. playingSongs.push(song);

  7. app.setGlobalData({ //如果是空的话,直接更新全局变量

  8. playList: playingSongs,

  9. playIndex: 0

  10. });

  11. } else { //不为空的话我们先判断当前列表是否包含选定歌曲

  12. for (var i = 0; i < playingSongs.length; i++) { //遍历整个列表

  13. var item = playingSongs[i];

  14. if (item.mid == song.mid) { //如果发现有mid相同的(即同一首歌)

  15. index = i; //获取这首歌在列表里的序号

  16. break;

  17. }

  18. }

  19. if (index != -1) { //歌曲已存在

  20. app.setGlobalData({

  21. playIndex: index //用我们获取的序号更新当前播放序号

  22. });

  23. } else { //不存在的情况

  24. playingSongs.push(song);

  25. index = playingSongs.length - 1; //将歌曲加入播放列表,播放序号改为列表最后一项

  26. app.setGlobalData({

  27. playList: playingSongs,

  28. playIndex: index

  29. });

  30. }

  31. }

  32. wx.navigateTo({

  33. url: '../play/play'

  34. });

  35. },

关于"微信小程序制作音乐播放器的代码怎么写"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"微信小程序制作音乐播放器的代码怎么写"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。

0