YouTube iframe embeds cannot autoplay on Android

六月ゝ 毕业季﹏ 提交于 2019-12-17 19:58:15

问题


I'm using a webView in my Android app to load YouTube iframe player and auto play videos. It works fine on Samsung Galaxy S2 & S3, but when runs on Samsung Galaxy S4, it always results in gray screen when trying to auto play.

On Galaxy S4, it works fine without autoplay, needs user action(click) to start playing (Nothing happened if adding "autoplay:1" in playerVars).

I tried to call player.playVideo() in onPlayerReady(), it resulted in this gray screen:

The LogCat also shows a weird error message:

E/IMGSRV(17004): :0: GetPTLAFormat: Invalid format

when failed to autoplay. I don't know what this message is about; I've googled it and found nothing.

Here's the Android code of WebView:

WebView wv = (WebView) findViewById(R.id.webview);
WebSettings websettings = wv.getSettings();
websettings.setJavaScriptEnabled(true);
websettings.setDomStorageEnabled(true);
websettings.setDatabaseEnabled(true);
wv.loadUrl(strUrl);
wv.setWebViewClient(new WebViewClient());
wv.setWebChromeClient(new WebChromeClient());
wv.setPadding(0, 0, 0, 0);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.setVerticalScrollBarEnabled(false);
wv.setHorizontalScrollBarEnabled(false);

Is this a known issue or if there's any solution to autoplay the video? Thanks!


回答1:


I think disabling autoplay is becoming a "standard".

  • In iOS Safari, autoplay in HTML5 tags is disabled. (http://developer.apple.com/library/safari/#documentation/AudioVideo/Conceptual/Using_HTML5_Audio_Video/Device-SpecificConsiderations/Device-SpecificConsiderations.html#//apple_ref/doc/uid/TP40009523-CH5-SW4)

  • In Chrome, autoplay is also disabled. (https://code.google.com/p/chromium/issues/detail?id=159336)

  • I believe the same happens with the recent versions of the default Android Web Browser.

By the way, you could try this

EDIT:

As the link is dead, I found it in https://lumi.do/p/04x626i1ngvht/how-to-autoplay-html5-video-on-android:

How to "autoplay" HTML5 video on Android
Written by Mathias Desloges in Labs on 19/05/11

Have you ever try to play with the new HTML5 video tag on an Android device?
We have and here is an stange issue which we faced.
Let's set up the context, we want our video starts playing just after the page load complete.
According to the HTML5 specification, we should use the "autoplay" attribute, but it has no effect in Android. So we tried with a little script tha call the play function after the page has loaded:

function callback () { document.querySelector('video').play(); } 
window.addEventListener("load", callback, false);

This reproduces well the behavior of the "autoplay" attribute on a classic desktop browser, but on an Android browser it fails.
we tried to attach the previously defined "callback()" function to a click event on a arbitrary node, and the video starts playing well "on click" (normal behavior). And while continue working on the page html code, we found the solution!

Don't ask me why, but after adding the "poster" attribute it works!




回答2:


I believe this is a symptom of the OS manufacturers becoming more restrictive in not letting videos autoplay due to bandwidth concerns. This has been an issue on iOS for some time where any JavaScript call to play() will fail unless the user has performed some action like a click first. That is likely what is going on in your case.




回答3:


As answered here I believe it's not just restricting auto-play but any auto events. For me it was a page transition in jquerymobile which was triggered when Cordova had loaded. I switched it to being triggered by a click even and this solved the problem.



来源:https://stackoverflow.com/questions/16416935/youtube-iframe-embeds-cannot-autoplay-on-android

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