Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bind 'timeupdate' with jquery on html5 audio element

I'm just trying to update a simple progress bar on time update, so I'm doing this:

var audioFile = thisPlayerBtn.attr('audioFile');
var audioFilePlayer = document.createElement('audio');
audioFilePlayer.setAttribute('src', audioFile);
audioFilePlayer.setAttribute('id', 'theAudioPlayer');
audioFilePlayer.load();

$.get();

audioFilePlayer.addEventListener("load", function() {
    audioFilePlayer.play();
}, true);       

$('#hiddenAudioElements').append(audioFilePlayer);
audioFilePlayer.play();
audioFilePlayer.bind('timeupdate', updateTime);


var updateTime = function(){
    var thisPlayer = $(this);
    var widthOfProgressBar = Math.floor( (100 / thisPlayer.duration) * thisPlayer.currentTime);
    $('#songIdForPorgessBar').css({
        'width':widthOfProgressBar+'%'
    });
}

Firebug says that "bind" is not a function, so I swapped it with "addEventListener" and I get no error but the progress bar doe not update.

Not sure what I'm doing wrong or if there's a better way of going about it. Here is my fiddle: http://jsfiddle.net/j44Qu/ it works, plays the audio, just doesn't update the progress bar and I'm stumped.

like image 452
user1053263 Avatar asked Oct 19 '25 14:10

user1053263


1 Answers

Your problem is that you're using jQuery objects when you should be using dom nodes and vice versa.
bind is a jQuery method yet you call it on the audio node

$(audioFilePlayer).bind('timeupdate', updateTime);

duration and currentTime are audio node properties but you try to reference them from a jQuery object

var widthOfProgressBar = Math.floor( (100 / this.duration) * this.currentTime);

http://jsfiddle.net/j44Qu/1/

like image 145
Musa Avatar answered Oct 21 '25 03:10

Musa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!