微信小程序音乐播放器:如何在页面退出时停止播放音乐
在微信小程序中,使用 wx.createInnerAudioContext() 创建音频实例时,如果需要在页面退出时停止播放音乐,可以使用 wx.stopBackgroundAudio() 方法。
具体实现如下:
onLoad(options) {
//从全局变量中取出来
this.data.songs = getApp().globalData.songs;
var index = options.index;
this.data.index = index;
console.log(options);
//获取歌手歌曲名
this.setData({
song: this.data.songs[index],
index: index
});
this.play(this.data.songs[index].id);
},
var player = wx.createInnerAudioContext();
// 在页面退出时停止播放
onUnload() {
wx.stopBackgroundAudio();
}
在 onUnload 方法中调用 wx.stopBackgroundAudio(),即可在页面退出时停止音乐播放。
注意:
wx.stopBackgroundAudio()只能停止使用wx.playBackgroundAudio()或wx.createInnerAudioContext().play()播放的音频。- 如果使用其他方法播放音频,则需要使用对应的方法停止播放。
原文地址: https://www.cveoy.top/t/topic/oRfy 著作权归作者所有。请勿转载和采集!