Android “ended” and “pause” events on <video>

In working on implementing playlists for the Brightcove Smart Players, I discovered some pretty annoying discrepancies between the HTML5 video specification and how Android handles video objects.  I’ve created a droidfix GitHub repository which captures an example of how to solve these problems.

The Problems

On Android devices, similar to the iPhone and iPod touch, video is a modal experience; that is, rather than playing the video within the context of the page, the video is immediately expanded to a full-screen experience. We can debate whether this approach is right or wrong all day long if you’d like; but for now let’s just accept it and move on.

On the Android platform, the video player is opened as if it were a new page. To return to the page from the playing (or paused) video, the user must use the device’s back button.  Several things do (or don’t) happen when you hit the back button:

  1. An “ended” event fires
  2. The currentTime property of the video is reset to 0
  3. A “pause” event does not fire

The ended event is particularly troubling when building a playlist-driven player as the ended event can be used to signal to the playlist that it’s time to load the next video. It creates a funny (for the developer; probably not the user) inescapable scenario when the user pauses the video and the next item on the playlist loads immediately until the end of the playlist is reached.

Workaround

I’ll point you to the GitHub version of droidfix.js to see the code; including an example page showing the code in action. Here’s a quick description of the workaround:

First, when timeupdate events are fired, we store the furthest forward position in the video that has played. We have to do this because of item #2 above; once the video actually ends, we’ll lose the last time index.

Now, when an ended event is received, we check to ensure the video has really reached the state specified for an ended event. In other browsers, we check that the currentTime property of the video is equal to the video’s duration; if that’s true, we’ve reached the end of the video.  For android, we check that the furthest forward position variable we stored earlier is within 2 seconds of the end of the video. Why 2 seconds? Well, the Android browser doesn’t fire a timeupdate event at the end of the video; furthermore, it also doesn’t seem to be consistent as to how far from the end of the video it fires its last timeupdate. In my testing, 2 seconds was a long enough buffer to include all examples; it also has the side-effect of being short enough that we can reasonably assert that beyond that point, the user has “completed’ the video.

If we determine that the ended event is legit, we fire a series of custom listeners set up in droidfix. However, if we determine this is android telling us that the video has ended when it hasn’t, we create a custom pause event and fire it so handlers bound to pause will be triggered.

Go fork and wreak havoc!

I put the example code on GitHub with a pretty descriptive README of how it works along with the current known issues / caveats. Feel free to grab it, modify, comment, etc. If nothing else, it provides an example that you can integrate with your own video solutions.

Dear MSFT and AAPL, Embrace Theora!

I swear; I’m not a hypocrite. I 100% stand by what I wrote to Mozilla about supporting H.264 or risking killing video on the Web sans plugins. I also continue to believe that H.264 is a significantly superior codec to Theora (it’s a fact so it’s hard to deny). And I also continue to believe that publishers with huge libraries of H.264 encoded video don’t want to go through the process of re-encoding all that video; not to mention the added expense of storing and distributing multiple formats. Mozilla, I say to you again: you need to support H.264 video (be it natively or through system libraries such as Quicktime or Gstreamer).  The Web needs H.264 to be universally available because the largest content producers need the highest quality codec to distribute their video to everyone; they need a codec with broad hardware support such that mobile devices don’t run themselves dead in 5 minutes of video playback. For now, and for the foreseeable future, H.264 is that codec.

Perhaps the greatest misunderstanding of my previous article was that I was somehow saying that Mozilla should drop support for Theora. Despite the number of times I refuted this claim, it seemed to persist. For the record, I do not think Mozilla should drop support for Theora in Firefox; I think they should add support for H.264 .

The Pareto Principle, if you subscribe to it, tells us that 80% of video content is going to be generated by 20% of the market. That 20% simply won’t settle for an inferior codec in the interest of that codec being free. For all of Mozilla’s posturing and idealism, the only real outcome they can achieve is that some 80% of the most viewed online video content will continue to be delivered via Flash, Silverlight, or some other plugin. Of course, that leaves us with all the video content that other 80% of content producers generate; and that’s where Theora comes in.

It’s All About the Tail!

The Web has given everyone a voice (I certainly could never have written an article that was read by over 10,000 people in less than 24 hours without the Web). The Long Tail Phenomenon a truly wonderful and powerful force; independent content producers now have the ability to distribute their content to a huge audience. The aspiring filmmaker can post his video on the Web; the aspiring musician can post her music on the Web. Bloggers are treated more and more like first-class journalists every day. And the potential for exposure is certainly far beyond what could have been achieved independently even 10 years ago.

But members of the Long Tail have very different needs and expectations for their content than the major content providers. For starters, the aspiring filmmaker probably doesn’t have the $10,000 per year royalties that could (theoretically) be due for his video encoded in H.264 and distributed on the Web. He also likely doesn’t have a lawyer to help him understand the obscure patent laws that cover H.264 usage. And he certainly doesn’t have the resources to fight the potential legal battle if he’s found to (even accidentally) misuse his “rights” to H.264.

This niche is where free formats demonstrate their great value; and speaking for the likely thousands of these hopeful filmmakers out there, we need a format that we can be confident will always be free. We need a format that we are free to do whatever we want with. We need a format that enables everyone to create their content and share it with the world freed from any legal concerns. For now, and for the foreseeable future, Theora is that codec.

No, the quality’s not nearly as good; but it’ll work for the guy just trying to get his film out there (Conversely, it definitely won’t work for FOX for putting HD episodes of 24 and House online). The point is, for people in the Long Tail, sometimes “good enough” really is good enough.

Microsoft and Apple Agree?

These guys are never on the same page so this must be big! Neither Microsoft or Apple are supporting Theora in their browsers. My thought is: Apple? Really? “Best friend” of the Indie Producer? A company that has built a reputation for providing fantastic support for creative professionals in both the audio and video industry should know better! As for Microsoft; well, nothing that company screws up surprises me, honestly.  There’s simply no excuse for not supporting Theora (on the desktop; Mobile is a different matter altogether due to the aformentioned availability of hardware decoders for H.264). The code is out there; there are implementations for every major OS. There’s nothing preventing either company from supporting it other than their own stubbornness; very much like Mozilla’s own stubbornness regarding H.264.

It’s About Producer Choice

This isn’t about consumer choice; it’s very clear that consumers just don’t care. It’s about choice for the content producers. It’s about letting the guy who maxxed out 10 credit cards to scrape together a movie (and unlike Kevin Smith, got rejected by Sundance) get his film on the Web for the world to see knowing that he’s not in danger of racking up even more debt.  It’s about embracing the long tail; the Here Comes Everybody mentality.

The Web needs H.264. The major broadcast and cable TV networks and the other big names of content will never choose Theora (I reiterate, they’d rather deliver their video via Flash to take advantage of H.264 than use Theora). For HTML5 video to succeed Firefox must support H.264.

Microsoft and Apple, it’s time for you two both to ensure that the independent content producers of the world get equal treatment in your browsers. I applaud you for accepting H.264 so that we could all get our favorite TV shows and movies online without plugins. Now I’m asking you to make sure we can also get content from smaller sources like The Guild and Red vs. Blue. For the Long Tail to continue to grow as a source of meaningful and influential content, we also need free video; and for that, we need Theora.