Search code examples
javascripthtmlaudio

HTML audio can't set currentTime


I am using Chrome. In my dev tools console, I tried the following:

enter image description here

Everything works as expected except last line. Why can't I set currentTime on it?

Also in general, I am finding this whole HTML5 Audio thing to not be very reliable. Is there a robust javascript wrapper that fallsback to flash ?


Solution

  • You need to do something like this (if you use jQuery)

    $('#elem_audio').bind('canplay', function() {
      this.currentTime = 10;
    });
    

    or in Javascript

    var aud = document.getElementById("elem_audio");
    aud.oncanplay = function() {
        aud.currentTime = 10;
    };
    

    The reason behind for this setup is you need to make sure the audio is ready to play.