HTML5 mediaElement.currentTime error

Recently, while working on a html5 video demo, I ran into an exception I could not figure out.

What I am doing is simply trying to change the video’s current time, but kept receiving an INVALID_STATE_ERR exception… It’s hard to get info on this, but I found “Will throw an INVALID_STATE_ERR exception if there is no selected media resource” in this page. Not much help, as I knew for a fact the media resource existed, but whatever selected media meant, I don’t know yet.

Here is an example of the problem:

<script>
  document.getElementById("video").currentTime = 2;
  document.getElementById("video").play();
</script>

Something as simple as this causes the exception, and I still don’t know why… but I did fix it.

A fix looks like this:

<script>
  var update = function() {
    if (document.getElementById("video").currentTime < 2) 
    {
      document.getElementById("video").currentTime = 2;
    }
  };
  document.getElementById("video")
    .setAttribute("ontimeupdate", "update();");
  document.getElementById("video").play();
</script>

All I do here is modify the currentTime form inside an event listener, and it works…

I think this has to do with what parts of the video are not yet loaded.

Advertisements

6 Responses to “HTML5 mediaElement.currentTime error”

  1. It might help to wait for the DOM-ready or body onload event to do anything with the video. I haven’t run into this problem yet and that’s what I’m doing.

  2. Thanks!

    Yeah, I was trying various strategies, which is how I ended up with what I ended up with, but I do not quite have it right yet.

  3. Rafael Bueno Says:

    Thanks!

  4. Try something like that:
    function load(event)
    {
    document.getElementById(“video”).currentTime = 2;
    }

    document.getElementById(“video”)
    .addEventListener(‘loadedmetadata’, load, false);

    This had helped in my case

  5. Yeah, I ended up solving it with ‘loadedmetadata’ as well. I should point out I have a post here: https://scottdowne.wordpress.com/2010/08/17/no-more-exceptions/ with the proper solution for anyone else who stumbles upon this.

  6. thanks, i am running into this problem with an audio element. It’s been driving crazy!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: