用户
 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
  • TA的每日心情
    郁闷
    2017-6-29 17:34
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    76

    主题

    104

    帖子

    10万

    金钱

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    0

    优秀版主

    2017-1-9 16:11:53 cyk 管理员 楼主 122644
    本帖最后由 cyk 于 2017-1-9 16:25 编辑

    微信小程序demo:QQ音乐;音乐搜索,音乐列表及播放停止







    代码示例:
    [AppleScript] 纯文本查看 复制代码
    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 ? ('0'+ parseInt(duration/60)) : (parseInt(duration/60));
            let second = duration%60 < 10 ? ('0'+duration%60) : (duration%60);
            str = minute+':'+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();
        }
    })


    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    使用道具 举报 回复
    啥也不说了,感谢楼主分享哇!
    啥也不说了,感谢楼主分享
    - 本文出
    感谢楼主的分享。
    啥也不说了,感谢楼主分享

    感谢楼主分享
    42若2日34234234
    66666666666666666
    6666666
    使用道具 举报 回复
    6666666666666666666666666
    12下一页
    发新帖
    您需要登录后才可以回帖 登录 | 立即注册
    咫尺论坛|小程序开发社区
    X

    扫码添加专属客服即可随时咨询

    还可领取小程序推广攻略