No more exceptions!

Yesterday’s post, I was talking about an exception I was receiving while trying to change an HTML5 video’s current time. I ended up solving it by putting it into an event listener, which worked, and made me suspect it was a loading issue, as event listeneres either fire after a delay of some sort. In the case of my previous solution, I would fire during “ontimeudate”. Looked 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>

This worked, but was not the best way.

After getting advice from a blog comment, a link from Dave, and a demo. The blog comment mentioned what I was suspecting, which had to do with loading. The link from Dave was the firefox source of what caused the error. This ended up saying a mDecoder was null, which would throw the error. The mDecoder was being set inside a function called “FinishDecoderSetup”, which may be called after something is loaded… ok, I will admit this code is too much to completely understand in one shot, but it does contain all my answers, so I will use it as a worst case scenario. For now, I will try the easier leads, and work my way down. Now, the demo’s source code basically was using currentTime while inside the ‘loadedmetadata’ event listener.

<script>
  document.getElementById("video").addEventListener('loadedmetadata', function() {
    this.currentTime = 3;
    this.play();
  }, false);
</script>

Oddly enough, I thought I tried this… but something external was probably mucking up the works. Lesson to be learned here is make reduced test cases to test, help you understand, and to help others understand you.

Advertisements

One Response to “No more exceptions!”

  1. Just wanted to point out that your example code is inefficient. You should use something like this, instead:

    document.getElementById(“video”).addEventListener(“loadedmetadata”, function () {
    this.currentTime = 3;
    this.play();
    }, true);

    Alternatively, store the DOM element in a variable:

    var video = document.getElementById(“video”);

    video.addEventListener(“loadedmetadata”, function () {
    video.currentTime = 3;
    video.play();
    }, true);

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: