2016-01-20 36 views
1

我有一段音頻自動啓動並在後臺循環。設置音頻標記迴路之間的延遲

即。

<audio autoplay loop> 
    <source src="bee_fly_by.mp3" type="audio/mpeg"> 
</audio> 

有沒有一種方法可以在播放之前設置延遲?所以我需要在循環之間說出10秒的差距。

+0

有你應該能夠幾種方法請執行此操作。有關更多詳細信息,請檢查HTML音頻/視頻DOM。 [link](http://www.w3schools.com/tags/ref_av_dom.asp)。你可以調用一個函數,當它結束時用一個10秒的定時器調用'pause'。有一個函數來檢查結束以及。 –

回答

0

這真的不是很辛苦,你可以得到非常看中這一點,但setTimeout的一個簡單的解決方案應該是非常簡單的在這裏。

假設jQuery是這個頁面上,這樣的事情下面應該工作(與您的音頻剪輯ID修改,如果超過一個頁面上):

function wait() { 
    console.log("audio clip ended, waiting 10 seconds to replay...."); 
    setTimeout(play, 10000); 
} 

function play() { 
    $("audio")[0].play(); 
    $("audio").on("ended", wait); 
} 

$(document).ready(function() { 
    play(); 
}); 
+0

工作得很好 - 謝謝。 –

0

音頻元素有ended事件。但是當視頻循環播放時,結束的事件永遠不會觸發。

所以你應該爲視頻循環編寫代碼,就像這樣。

var audioControl = document.getElementById("myAudio"); 
audioControl.onended = function(){ 
    this.currentTime = 0; 
    var delay = setTimeout(function(){ 
    audioControl.play(); 
    clearTimeout(delay); 
    }, 5000); 
} 

工作小提琴:https://jsfiddle.net/5ggmdg7m/