I need to tell, whether video cannot be played (\"x\" sign is shown in browser).
This code does\'t works. \"onerror\" event will never be fired under Firefox
To catch error event, you should use video.addEventListner()
:
var video = document.createElement('video');
var onError = function() { // your handler};
video.addEventListener('error', onError, true);
...
// remove listener eventually
video.removeEventListener('error', onError, true);
Note that the 3rd parameter of addEventListener
(on capture) should be set to true. Error event is typically fired from descendatns of video element ( tags).
Anyway, relying on video tag to fire an error
event is not the best strategy to detect if video has played. This event is not fired on some android and iOS devices.
The most reliable method, I can think of, is to listen to timeupdate
and ended
events. If video was playing, you'll get at least 3 timeupdate events. In the case of error, ended
will be triggered more reliably than error
.