javascript - Feature detect if user gesture is needed - Stack Overflow

is there a way to detect if calling play() on a video element is allowed without a user gesture?On And

is there a way to detect if calling play() on a video element is allowed without a user gesture? On Android Chrome this warning is given:

Failed to execute 'play' on 'HTMLMediaElement': API can only be initiated by a user gesture.

So on Chrome Android a user gesture is required to start the playback of a video, while it isn't on desktop Chrome. Is there a way to detect which behavior I will get?

I want to have slightly different behavior in my app depending on if calling play programatically is allowed or not.

I have tried to use Modernizr.videoautoplay, but that checks if the autoplay property on the element, which is not the same thing. This gives false negatives for IE11 and Edge.

Edit: added an example. The video will start playing automatically in Chrome desktop and IE11 or Edge (with 3s delay) on windows 8 or 10. For Chrome@Android a user interaction is needed (clicking the button) and the error message can be seen in the console.

is there a way to detect if calling play() on a video element is allowed without a user gesture? On Android Chrome this warning is given:

Failed to execute 'play' on 'HTMLMediaElement': API can only be initiated by a user gesture.

So on Chrome Android a user gesture is required to start the playback of a video, while it isn't on desktop Chrome. Is there a way to detect which behavior I will get?

I want to have slightly different behavior in my app depending on if calling play programatically is allowed or not.

I have tried to use Modernizr.videoautoplay, but that checks if the autoplay property on the element, which is not the same thing. This gives false negatives for IE11 and Edge.

Edit: added an example. The video will start playing automatically in Chrome desktop and IE11 or Edge (with 3s delay) on windows 8 or 10. For Chrome@Android a user interaction is needed (clicking the button) and the error message can be seen in the console.

Share Improve this question edited Sep 21, 2015 at 15:15 oskbor asked Sep 18, 2015 at 13:39 oskboroskbor 1,59218 silver badges35 bronze badges 5
  • can you post a code example that has that error? – Patrick Commented Sep 20, 2015 at 6:47
  • Hi @Patrick I have updated the question with a link to an example. Its a bit plex (adding frames to the video bit by bit) but the issue can be seen – oskbor Commented Sep 21, 2015 at 15:19
  • Hi @oskbor, did you get a fix for this? – Novice Commented Dec 15, 2015 at 17:26
  • Nope, did not find a way to detect that – oskbor Commented Dec 15, 2015 at 17:27
  • I ended up just filtering var isMobile = false; if ((/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(navigator.userAgent.toLowerCase())) || (/Mobi/i.test(navigator.userAgent.toLowerCase()))) isMobile = true; – MrPHP Commented Apr 17, 2016 at 7:24
Add a ment  | 

1 Answer 1

Reset to default 5

The play method returns a promise which can be used to catch the error.

Not all browsers follow the specification so you will have to check if what is returned is a promise first.

var autoPlayAllowed = true;
var promise = document.createElement("video").play();
if(promise instanceof Promise) {
    promise.catch(function(error) {
        // Check if it is the right error
        if(error.name == "NotAllowedError") {
            autoPlayAllowed = false;
        } else {
            throw error;
        }
    }).then(function() {
        if(autoPlayAllowed) {
            // Allowed
        } else {
            // Not allowed
        }
    });
} else {
    // Unknown if allowed
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744321762a4568452.html

相关推荐

  • javascript - Feature detect if user gesture is needed - Stack Overflow

    is there a way to detect if calling play() on a video element is allowed without a user gesture?On And

    7天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信