争怎路由网:是一个主要分享无线路由器安装设置经验的网站,汇总WiFi常见问题的解决方法。

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

时间:2024/3/21作者:未知来源:争怎路由网人气:

微信小程序,简称小程序,英文名Mini Program,是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或搜一下即可打开应用。小程序是一种不用下载就能使用的应用,也是一项门槛非常高的创新,经过将近两年的发展,已经构造了新的小程序开发环境和开发者生态。
想做一个功能齐全的类似QQ音乐的微信小程序,demo来了,可供大家参考,包含音乐搜索、音乐列表及播放停止
示例代码:

QQ图片20170320144615.png

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();
    }
})

以上就是QQ音乐微信小程序实例代码的详细内容,更多请关注php中文网其它相关文章!


小程序是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或者搜一下即可打开应用。



关键词:QQ音乐微信小程序案例代码




Copyright © 2012-2018 争怎路由网(http://www.zhengzen.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版