2 回复
请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
<image src="https://qiniu-image.qtshe.com/20210719music.png" class="music-icon {{start ? '' : 'active'}}" bindtap="handleProxy" />
onReady() {
this.back = wx.getBackgroundAudioManager()
// 对实例进行设置
this.back.src = "https://qiniu-video.qtshe.com/qtshe.mp3"
this.back.title = '青团社8周年庆' // 标题为必选项
this.back.play() // 开始播放
// 1、onEnded监听播放自然结束
this.back.onEnded(() => {
// 2、必须重新设置src才能循环之后会重新自动播放
this.back.src = "https://qiniu-video.qtshe.com/qtshe.mp3"
})
this.back.onStop((res) => {
this.setData({ start: !this.data.start })
this.close = true
this.back.pause();
})
},
handleProxy() {
this.back.pause(); // 点击音乐图标后出发的操作
this.setData({ start: !this.data.start })
if (this.data.start) {
if (this.close) {
this.close = false
this.back.src = "https://qiniu-video.qtshe.com/qtshe.mp3"
} else {
this.back.play();
}
console.log("背景音乐已开启");
} else {
this.back.pause();
console.log("背景音乐已暂停");
}
},