用户
 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,登录网站

1307

主题

4248

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
13552
2017-1-6 19:31:09 天下雪原创达人第一期 轮子比赛第一期 管理员 楼主 2061631

1.png 2.png 3.png 4.png 5.png


代码示例:
[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();
	}
})

项目地址及下载:
游客,如果您要查看本帖隐藏内容请回复

binxchen1010 发表于 2017-1-6 21:38
请继续上传好的demo,我会不断的支持并且为作者打气

感谢支持,欢迎同学在 别人需要demo的时候宣传一下我们
请继续上传好的demo,我会不断的支持并且为作者打气
正在找这个  学习了
好东西,学习
项目地址过来看看
感谢楼主无私分享
谢谢分享
不断的关注  学习
[demo]微信小程序demo:QQ音乐;音乐搜索,音乐列表及播放停止
赞一个
刚开始学习,看看,赞一个
学习
您需要登录后才可以回帖 登录 | 立即注册