2012-08-31 83 views
4

我无法让HTML5音频在我的网站上循环播放:http://oclock.webs.com。 (这是一个闹钟,当闹钟响起时,应该一遍又一遍地循环播放1秒的闹钟声,直到您在警告框中单击确定)。HTML5音频循环出现问题


简单地在音频标签中包含“loop”没有做任何事情。

我发现在计算器上一个有用的后导致我到这个网站:http://forestmist.org/2010/04/html5-audio-loops/

我知道方法1和,因为他们没有在Firefox,并且是前途未卜在Chrome 2不会为我工作,所以方法3应该已经工作,不幸的是它没有。当我在该网站的页面上时,方法3工作,但当代码包含在我自己的页面中时,方法3不起作用,所以我认为可能存在一些冲突的代码。


我的网页的

相关部分:

<audio id="audio1" preload> 
    <source src="media/alarm.ogg" type="audio/ogg" /> 
    <source src="media/alarm.mp3" type="audio/mpeg" /> 
</audio> 

<audio id="audio2" preload> 
    <source src="media/alarm.ogg" type="audio/ogg" /> 
    <source src="media/alarm.mp3" type="audio/mpeg" /> 
</audio> 

相关页面的JavaScript:

if(timer==true && hours==hv && mins==mv && secs==sv){ 
    document.getElementById("audio1").play(); 
    alert("o'clock: Alarm ringing!"); 
    document.getElementById("alarm").value="Set Alarm"; 

    document.getElementById('audio1').addEventListener('ended', function(){ 
     this.currentTime = 0; 
     this.pause(); 
     document.getElementById('audio2').play(); 
    }, false); 

    document.getElementById('audio2').addEventListener('ended', function(){ 
     this.currentTime = 0; 
     this.pause(); 
     document.getElementById('audio1').play(); 
    }, false); 
} 

else{ 
    document.getElementById("audio1").pause(); 
    document.getElementById("audio2").pause(); 
    document.getElementById("audio1").currentTime = 0; 
    document.getElementById("audio2").currentTime = 0; 
} 

意向:在计算机的时间匹配的选定时间在页面上时,音频报警将重复(循环),直到用户在弹出的警告框中单击确定。

问题:目前它只播放音频一次,它不会循环播放。我在哪里搞砸了?

+0

它也可以帮助我们,如果你可以对准你的大括号。 – KyelJmD

+0

谢谢@KyelJmD,补充了一些意向。 – zants

回答

0

“结束事件”或“循环”属性在某些浏览器中不起作用。 所以我用window.setInterval()来循环播放音频。

对于此方法,您应该了解高级音频文件的播放时间。

function playAudioWithLoop (/**HTMLAudioElement*/audio, /**Number*/length) 
{ 
    function onEnd(){ 
     audio.play(); 
    } 

    /* length is msec */ 
    if(length > 0) { 
     audio.__timer = window.setInterval(onEnd, length); 
    } 
} 

function stopAudioWithLoop (/**HTMLAudioElement*/audio) 
{ 
    if(audio.__timer) { 
     audio.__timer = window.clearInterval(audio.__timer); 
    } 
} 

=========================================== ===========================

***playAudioWithLoop(document.getElementById("audio1"), 12*1000);*** 
***stopAudioWithLoop(document.getElementById("audio1"));*** 
+0

我不完全确定如何实现并编辑它。 我是否期望用此代替当前的代码,或者除了我目前的代码? 在给定的代码中究竟应该编辑什么(如果有的话)? – zants