3

What does the exception mean? How can I fix it? I am using the latest Google Chrome for Ubuntu.

Josh Unger
  • 6,717
  • 6
  • 33
  • 55
jdw
  • 1,533
  • 3
  • 17
  • 26
  • What browser are you using? Which version? – Bodman Aug 29 '11 at 04:58
  • 3
    In Chromium, the call to `videoElement.webkitEnterFullScreen()` needs to be initiated by a user action such as a button click. You'll get this error if your JS attempts to fullscreen a video without it being user-driven. – ebidel Aug 29 '11 at 05:14

1 Answers1

5

INVALID_STATE_ERR: DOM Exception 11 can occur when a call to webkitEnterFullscreen is made before the video element has received its asset's metadata. The simplest solution is to defer the invocation of webkitEnterFullscreen by putting it in a callback function assigned to the video's loadedmetadata event.

In a mobile environment, you need to take things a step further by attaching that call to a touchable element so that it is user initiated since play and fullscreen actions must be driven by user interaction in mobile environments.

The code should look kind of like this:

var video, play, fullscreen;

video = document.createElement('video');
video.src = 'my_cool_video.mp4';
video.addEventListener('loadedmetadata', function () {
    fullscreen.disabled = false;
}, false);

play = document.createElement('button');
play.innerHTML = 'PLAY';
play.addEventListener('click', function () {
    video.play();
}, false);

fullscreen = document.createElement('button');
fullscreen.innerHTML = 'FULLSCREEN';
fullscreen.disabled = true;
fullscreen.addEventListener('click', function () {
    video.webkitEnterFullscreen();
}, false);

document.body.appendChild(video);
document.body.appendChild(play);
documnet.body.appendChild(fullscreen);
natlee75
  • 5,097
  • 3
  • 34
  • 39