4

我有下一个代码

var audioElement0 = document.createElement('audio');
audioElement0.setAttribute('src', 'notify.wav');
audioElement0.setAttribute('autoplay', 'autoplay');
audioElement0.Play(); 

var audioElement1 = document.createElement('audio');
audioElement1.setAttribute('src', 'notify.wav');
audioElement1.setAttribute('autoplay', 'autoplay');
audioElement1.Play(); 

var audioElement2 = document.createElement('audio');
audioElement2.setAttribute('src', 'notify.wav');
audioElement2.setAttribute('autoplay', 'autoplay');
audioElement2.Play(); 

但它只播放一次......我该如何解决?

4

3 回答 3

18

你有循环属性:

audioElement.loop=true;

但是有些浏览器不支持循环属性,你可以像这样添加一个事件监听器:

audioElement.addEventListener('ended', function() {
    this.currentTime = 0;
    this.play();
}, false);
于 2012-11-28T17:13:38.490 回答
2

audio元素有一个loop布尔属性,您可以将其设置为自动循环播放。另一种选择是添加响应音频元素的“结束”事件的事件侦听器。在您的处理程序中,将音频的位置设置回 0,然后重新播放。

于 2012-11-28T17:12:22.213 回答
1

在音频标签中设置loop="loop"属性。

于 2012-11-28T17:12:48.583 回答