QQ音樂微信小程式實例碼

高洛峰
發布: 2017-03-20 15:16:49
原創
7222 人瀏覽過

想做一個功能齊全的類似QQ音樂的微信小程序,demo來了,可供大家參考,包含音樂搜尋、音樂清單及播放停止
範例程式碼:

QQ音樂微信小程式實例碼

var util = require('../../utils/util.js')

var app = getApp()

Page({
    data: {
        playingSong: {},
        songUrl: '',
        songImg: '',
        songState: {
            progress: 0,
            currentPosition: '00:00',
            duration: '00:00'
        },
        isPlaying: true,
        lyric: ''
    },
    onLoad: function(){
        console.log('playsong onLoad');

        let that = this;
        let songdata = app.globalData.songData;
         
        that.setData({
            playingSong: songdata,
            songUrl: 'http://ws.stream.qqmusic.qq.com/C100' + songdata.songmid + '.m4a?fromtag=38',
            songImg: 'http://y.gtimg.cn/music/photo_new/T002R150x150M000' + songdata.albummid + '.jpg',
        });

        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('playsong onReady');
        let that = this;
        that.songPlay();

        wx.onBackgroundAudioPlay(function(){
            console.log('播放了');
            that.songPlay();
        });
    },
    timeToString: function(duration){
        let str = '';
        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音樂微信小程式實例碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!