方法一:修改浏览器配置
代码
<audiocontrols="controls"hiddensrc="../../static/alert.mp3"ref="audio"
></audio>this.$refs.audio.currentTime = 0; //从头开始播放提示音
this.$refs.audio.play(); //播放
chrome配置允许声音自动播放
- 设置
- 搜索”
网站设置
“
- 下拉在”
更多内容设置
“找到“声音”
,选择网站可以播放声音或者在自定义内添加
方法二:引导用户点击
<audiocontrols="controls"hiddensrc="../../static/alert.mp3"ref="audio"
></audio>init() {//刚打开网站时弹框引导用户点击this.$alert("允许网站播放声音", "提示", {confirmButtonText: "确定",showClose: false,callback: action => {this.isPlay = true;this.play();}});
},
play() {//用户点击后,即可播放if (this.isPlay) {this.$refs.audio.currentTime = 0; //从头开始播放提示音this.$refs.audio.play(); //播放}
},