首页 > 微信应用  > 

QQ音乐微信小程序实例代码

QQ音乐微信小程序实例代码
这篇文章介绍QQ音乐微信小程序实例代码

想做一个功能齐全的类似qq音乐的微信小程序,demo来了,可供大家参考,包含音乐搜索、音乐列表及播放停止示例代码:

var util = require(&#39;../../utils/util.js&#39;)var app = getApp()Page({ data: { playingSong: {}, songUrl: &#39;&#39;, songImg: &#39;&#39;, songState: { progress: 0, currentPosition: &#39;00:00&#39;, duration: &#39;00:00&#39; }, isPlaying: true, lyric: &#39;&#39; }, onLoad: function(){ console.log(&#39;playsong onLoad&#39;); let that = this; let songdata = app.globalData.songData; that.setData({ playingSong: songdata, songUrl: &#39;http://ws.stream.qqmusic.qq.com/C100&#39; + songdata.songmid + &#39;.m4a?fromtag=38&#39;, songImg: &#39;http://y.gtimg.cn/music/photo_new/T002R150x150M000&#39; + songdata.albummid + &#39;.jpg&#39;, }); let thatData = that.data; wx.playBackgroundAudio({ dataUrl: thatData.songUrl, title: thatData.playingSong.songname, coverImgUrl: thatData.songImg, success: function(res){ //do something } }); }, onReady: function(){ console.log(&#39;playsong onReady&#39;); let that = this; that.songPlay(); wx.onBackgroundAudioPlay(function(){ console.log(&#39;播放了&#39;); that.songPlay(); }); }, timeToString: function(duration){ let str = &#39;&#39;; let minute = parseInt(duration/60) < 10 ? (&#39;0&#39;+ parseInt(duration/60)) : (parseInt(duration/60)); let second = duration%60 < 10 ? (&#39;0&#39;+duration%60) : (duration%60); str = minute+&#39;:&#39;+second; return str; }, songPlay: function(){ let that = this; let inv = setInterval(function(){ wx.getBackgroundAudioPlayerState({ success: function(res){ if(res.status == 1){ that.setData({ isPlaying: true, songState: { progress: res.currentPosition/res.duration*100, currentPosition: that.timeToString(res.currentPosition), duration: that.timeToString(res.duration) } }) }else{ that.setData({ isPlaying: false }); clearInterval(inv); } } }); }, 1000); }, songToggle: function(){ let that = this; if(that.data.isPlaying){ wx.pauseBackgroundAudio(); }else{ wx.playBackgroundAudio({ title: that.data.playingSong.songname, coverImgUrl: that.data.songImg }); }; that.songPlay(); }})

QQ音乐微信小程序实例代码由讯客互联微信应用栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“QQ音乐微信小程序实例代码