User gesture required to start playback in Android HTML5 player

情到浓时终转凉″ 提交于 2020-01-21 11:16:12

问题


I am using the HTML5 video tag on android and sometimes the chrome browser says it requires an explicit user gesture/click to start playback:

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

I know this is a known issue in Android but what I don't understand is why sometimes it plays automatically and on other occasions it requires a user action!

I am using the video tag with autoplay option.


回答1:


As of January 24th, 2017

HTML5 video tags can autoplay on android if the video is muted. You would need to include the muted and autoplay attributes in the tag to make it work as intended.

Here is a link to the article: Autoplay on Chrome for Android as of version 53




回答2:


Auto-play is disabled since Android SDK 17 to avoid poor user experiences with video playback (i.e. undesired playback, undesired data usage). Normally video should only play following a user action. This is recommended behavior for both Android and iOS nowadays.

You can, however, set setMediaPlaybackRequiresUserGesture to false to enable auto-play if you really need to. Remember to check the SDK version, because this option does not exist before Android SDK 17.

int SDK_INT = android.os.Build.VERSION.SDK_INT;
    if (SDK_INT > 16) {
        engine.getSettings().setMediaPlaybackRequiresUserGesture(false);
}

There is a LONG discussion and debate about Google's decision regarding autoplay here: http://chromium-bugs.chromium.narkive.com/cW5IXVgj/issue-178297-in-chromium-android-chrome-does-not-allow-applications-to-play-html5-audio-without-an



来源:https://stackoverflow.com/questions/34295126/user-gesture-required-to-start-playback-in-android-html5-player

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