17
votes

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:

enter image description here

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!

3
Rather than an iFrame, have you checked out developers.google.com/youtube/android/player ?FunkTheMonk
The reason why we don't want to use this Youtube Android Player is because there is no any overlay allowed.Bagusflyer

3 Answers

12
votes

I think disabling autoplay is becoming a "standard".

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
votes

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.

2
votes

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.