HTML5 Audio - Sound only plays once
I've had similar problems replaying audio, but managed to solve it by calling the load()
method before the play()
call. Try this:
$('.sound').live('click', function () { var sound = $("#letter_audio")[0]; sound.load(); sound.play(); return false;});
I think this is better than resetting the src
attribute for two reasons:
- Keeps the
src
value in the page content, rather than in the JavaScript Works when you embed multiple
<source>
elements of different audio formats:<audio id="letter_audio"> <source src="audio/bomb.mp3" type="audio/mp3" preload="auto"/> <source src="audio/bomb.ogg" type="audio/ogg" preload="auto"/></audio>
try this.. basically, resetting the src
attribute should reset the audio as desired.
$('.sound').live('click', function () { var sound = $("#letter_audio")[0]; sound.src = "audio/bomb.mp3"; sound.play(); return false;});
it seems that for the time being the currentTime attribute is read-only (and in 2011 it has got a wont-fix status)
however, after setting src
on your audio-object (again) - like also proposed by @Lloyd - the currentTime attribute becomes settable (at least on a the current chrome-version for linux (24.0.1312.69)).
this means by adding the strange line
audio.src = audio.src
your example does what it is supposed to.
in full:
$('.sound').live('click', function () { var sound = $("#letter_audio")[0]; sound.src = sound.src; sound.pause(); sound.currentTime = 0; sound.play(); return false;});
as it is enough set the src
once, you might want to call it somewhere during the setup process.