How do I detect if the HTML5 autoplay attribute is supported?

前端 未结 9 745
遇见更好的自我
遇见更好的自我 2020-12-05 05:04

How do I best detect whether a browser\'s HTML5 video element supports autoplay?

On current iOS Safari, for example, autoplay is disabled.

Update:

9条回答
  •  暖寄归人
    2020-12-05 05:18

    A little late to the party, but the accepted solution seems outdated: Modernizr now has implemented this feature, see https://github.com/Modernizr/Modernizr/blob/master/feature-detects/video/autoplay.js

    Contains similar hacks to the other solutions posted here though, but as long as browsers don't expose availabilty of this feature, this seems to be the best solution for now.

    Note that this an async test available since Modernizr 3, so you have to use the following .on() syntax for your test:

    Modernizr.on('videoautoplay', function(result){
      if(result) {
        alert('video autoplay is supported');
      }  else {
        alert('video autplay is NOT supported');
      }
    });
    

    See for yourself: http://codepen.io/anon/pen/VYoWWY?editors=001

    The above sample includes Modernizr 3 with the 'videoautplay' feature detection (http://v3.modernizr.com/download/#-videoautoplay).

提交回复
热议问题