Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the currentTime of an <audio> tag not working?

I have this audio tag playing in the background, and I'm able to store the progress in seconds to a cookie. But in no way I'm able to start the audio from that cookie. (for continuing on other pages)

$("p#sound audio").currentTime = $.cookie("audioTime");

<audio autoplay="autoplay" loop="loop" ontimeupdate="document.getElementById('tracktime').innerHTML = Math.floor(this.currentTime); $.cookie('audioTime', Math.floor(this.currentTime));">
    <source src="audio/song.ogg" type="audio/ogg" />
    <source src="audio/song.mp3" type="audio/mp3" />
    Your browser does not support the audio tag.
</audio>
<span id="tracktime">0</span>

Does this have to do with the song being loaded again from start?

Thanks!

EDIT:

$("p#sound audio").get[0].currentTime

With .get[0], it doesn't work either.

Can someone please clear things up for me? Greatly appreciated!

like image 781
fishbaitfood Avatar asked Dec 23 '11 23:12

fishbaitfood


People also ask

How do I customize my audio tag?

HTML 5 audio tags can be styled. By using the audio tag with “controls” attribute, the default browsers player is used. You can customize by not using the browsers controls. You can also add CSS classes to each one of the elements and style them accordingly.

How do I get the current time from a sound tag?

getElementsByTagName("audio")[0]; var cur_time = myaudio. currentTime; $('#curPosition'). val(cur_time);


2 Answers

You can set the start time by adding t=<time> to the URL, as documented here: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_HTML5_audio_and_video#Specifying_playback_range

E.g. <audio src="http://example.com/audio.mp3#t=50></audio>

like image 158
Brian Beckett Avatar answered Nov 01 '22 02:11

Brian Beckett


You need to wait until audio source loads before you set the current time.

$(function(){
    $('audio').bind('canplay', function(){
        $(this)[0].currentTime = $.cookie('audioTime');
    });
});
like image 29
Brian Hadaway Avatar answered Nov 01 '22 01:11

Brian Hadaway