Efficiently detect if a device will play silent videos that have the autoplay attribute

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 00:52:40

You can detect video autoplay support by checking if the paused status of a video element changes after it programmatically plays. This alone can return false negatives in some mobile browsers, so a Promise check should be added in order to cover these.

This method works in all major browsers (desktop and mobile), except for Android <= 4.0 and Windows Phone, where it returns a false negative.

Here is the detect function:

var supports_video_autoplay = function(callback) {

  if (typeof callback !== "function") return false;

  var v = document.createElement("video");
  v.paused = true;
  var p = "play" in v && v.play();

  callback(!v.paused || ("Promise" in window && p instanceof Promise));

};

Usage:

supports_video_autoplay(function(supported) {
  if (supported) {
    // video autoplay supported!
  } else {
    // no video autoplay support :(
  }
});

Live test: https://codepen.io/paulorely/pen/QveEGy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!