uniapp js自动播放音频
·
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("静音设置失败");
},
});
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)