1、创建音频并播放
export default {
data() {
return {
audio: null
}
},
onLoad(option) {
// 获取音频文件路径
const audioPath = '/static/audio/warn.mp3';
// 创建并播放音频对象
this.audio = uni.createInnerAudioContext();
this.audio.src = audioPath;
this.audio.autoplay = true;
// 隐藏音频控件
this.audio.obeyMuteSwitch = false;
this.audio.loop = true;
this.audio.onError((res) => {
console.log('onError', res.errMsg);
});
},
onUnload() {
// 停止音频播放并销毁音频对象
this.audio.stop();
this.audio.destroy();
},
}
2、解决IOS无法播放音频问题
var music = wx.setInnerAudioOption({
obeyMuteSwitch: false,
success: function(res) {
console.log("开启静音模式下播放音乐的功能");
},
fail: function(err) {
console.log("静音设置失败");
},
});