If none of the < source > tags provided for an HTML5 < video > tag are playable, I want to display an error.
According to this page on the Mozilla Developer Network, it seems I have to check the networkState property of the video element to see if any sources loaded, as each seperate source tag throws its own error when it fails to load.
To detect that all child elements have failed to load, check the value of the media element's networkState attribute. If this is HTMLMediaElement.NETWORK_NO_SOURCE, you know that all the sources failed to load.
But at what point should I check the networkState? If I check immediately on calling video_tag.load(), it always tells me the networkState is NETWORK_NO_SOURCE even if the source elements are valid and the video plays fine. Therefore, I assume I hvae to wait until the source tags have been tried by the browser.
Here's the test:
var state = this._video_tag.networkState;
console.log( 'networkState', state );
if( state == this._video_tag.NETWORK_NO_SOURCE )
{
throw new Error( 'No valid sources' );
}
I have tried all of the following video element events with invalid source tags: loadstart, loadedmetadata, loadeddata & error with the following results (in Firefox):
- loadstart: called but networkState is NETWORK_LOADING
- loadedmetadata: not called
- loadeddata: not called
- error: not called
However, if I check out the video tag in Firebug, the networkState is NETWORK_NO_SOURCE just as expected.
What event should I be using to control when to check the video tag, or is there a better way of doing this?