Reputation: 1868
I want to make a player like winamp by html5 audio player. but when I click a javascript button('play' button which is used for onclick event to call functions to change the audio source and play music) then sometimes the source doesn't load in time. my code is:
<script type="text/javascript">
function play(src){
document.getElementById('new').currentSrc=src+".ogg";
document.getElementById('new').play();
}
</script>
<audio id="new">
<source src="m.ogg" type="audio/ogg" />
Your browser does not support HTML5 audio. Please upgrade your browser.
</audio>
<button onclick="play('source');">Play next song</button>
and another problem is there always an initial src need to keep there(like m.ogg).otherwise the new source doesn't load. but if I change innerHTML of 'new' audio player then this problem doesn't happen but its little slow to load.
so whats the best practice for this ? changing the current source or changing the innerHTML of the audio player?
-thanks.
Upvotes: 1
Views: 258
Reputation: 11
Try calling
document.getElementById('new').load();
before calling play().
IIRC this will attempt to play as soon as enough data has buffered.
Upvotes: 1