我尝试使用下面的代码在 JavaScript 中播放声音一段时间,但没有成功,声音只播放一次,问题是什么?
for(var i = 0; i < errors; i++){
PlaySound3();
}
功能:
function PlaySound3() {
var audioElement = document.getElementById('beep');
audioElement.setAttribute("preload", "auto");
audioElement.autobuffer = true;
audioElement.load();
audioElement.play();
};
HTML 代码:
<audio id="beep">
<source src="assets/sound/beep.wav" type="audio/wav" />
</audio>
如果你想无限播放声音使用该属性loop在标签音频中:
<audio id="beep" loop>
<source src="assets/sound/beep.wav" type="audio/wav" />
</audio>
Edit
如果你想在 3 次后停止循环,请添加一个事件监听器:
HTML:
<audio id="beep">
<source src="assets/sound/beep.wav" type="audio/wav" />
</audio>
JS:
var count = 1
document.getElementById('beep').addEventListener('ended', function(){
this.currentTime = 0;
if(count <= 3){
this.play();
}
count++;
}, false);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)