Detect youtube iframe autoplay failure

2019-06-01 11:10发布

问题:

iOS and Android only allow playing a video inside a callback of a user interaction. Chrome 66 introduced the autoplay policy.

How can we detect that embedded youtube iframe failed to autoplay a video so we can play it muted and show 'tap to unmute' button?

The API has onError callback but it's not clear if and what error will fire. This is also hard to test because of Chrome's Media Engagement Index.

HMTL 5 video autoplay failure can be detected. Is there an equivalent in the YouTube iframe API?

var promise = document.querySelector('video').play();

if (promise !== undefined) {
  promise.then(_ => {
    // Autoplay started!
  }).catch(error => {
    // Autoplay was prevented.
    // Show a "Play" button so that user can start playback.
  });
}

Thanks