user3213563
user3213563

Reputation: 27

HTML5 autoplay once

The following javascript function serves and autoplays a audio file (via a HTML 5 audio tag), cuts the mp3 playback at 6 seconds and loops + autoplays the audio from the beginning.

javascript:

function updateaudio() {    
var a_str = '<audio autoplay source src="audio/coolsound.mp3" type="audio/mpeg"></audio>';
document.getElementById('audio_span').innerHTML = a_str;
}
setInterval(updateaudio, 6000)

html:

<div><span id="audio_span"></span><script src="js/audio.js"></script></div>

Is there any using the html5 audio attribute to set autoplay playback to once off i.e. non-looping? Alternatively, is there another way to achieve this (via either javascript or html5)? Upon the audio event ending, I would like to set a flag and stop any playback.

Upvotes: 0

Views: 997

Answers (1)

MikeHelland
MikeHelland

Reputation: 1159

Don't use setInterval() if you don't want a loop.

If you simply want to pause the playback after six seconds do:

setTimeout( function () {

    document.getElementById('audio_span').pause();

}, 6000)):

Upvotes: 1

Related Questions