YouTube iframe embeds cannot autoplay on Android - android

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!

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!

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.

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.

Related

Youtube not playing in android's react-native WebView

I've got a problem playing Youtube(or any) video in react-native's webview on android device. iOS works just fine.
I've already looked at several SO related questions, but everyone seem to suggest two things:
webview.setWebChromeClient(new WebChromeClient()); which is as far as I am concerned default in react-native's webview since few versions later
enable hardware acceleration android:hardwareAccelerated="true" in AndroidManifest.xml (which I have done)
All I got is a rendered player with controls and everything, but black screen.

Android HTML5 video in WebView

I am attempting to play an HTML5 video within my WebView app. It works as expected on every device I have tested that is running Android 5.x, but does not work on any device running 4.x, meaning it essentially doesn't work at all.
I have turned on hardware acceleration and I have set a WebChromeClient as the docs say to do, but the video still will not play.
In order to support inline HTML5 video in your application, you need
to have hardware acceleration turned on, and set a WebChromeClient.
AndroidManifest.xml
<application>
android:hardwareAccelerated="true"
...
</application>
MyFragment.java
webView = new WebView(getActivity(), null, this);
webView.setWebChromeClient(new WebChromeClient());
Is there something else I need to do that is not documented in the developer reference?
The problem is WebKit poorly handles redirects for videos. There are videos within webpages from my company's proprietary API. When a video is clicked, the call goes to our API, then redirects to Amazon S3 to get the actual video file. WebKit then tries to "chunk" the video (instead of pre-loading the entire thing) as you would expect. However, S3 has already done that, which causes the playback to be completely broken.
Android 5.x works fine because WebView is based upon Chromium starting in 4.4, which handles the redirect appropriately.

Android: unable to play/pause html5 video programatically until manually started

I have an Android KitKat application with an embedded WebView. The page contains an html5 video element. The video loads fine, but play() has no effect until I tap the play button on the screen, and then play() and pause() work fine. I have also tried myWebView.setWebChromeClient(new WebChromeClient());, but this makes no difference. Also, this same page works fine in Chrome browsers on Mac and Linux.
Well, I found the problem. Android 4+ will not allow autoplay of video or programmatic start of play without first having the user tap the play button on the screen. This is so the user will implicitly acknowledge the use of bandwidth by manually activating playback. While I understand why this would be reasonable for a phone user with a cell connection, my application is a television with an Android HDMI stick. This is a very unfortunate restriction. See more here: Autostart html5 video using android 4 browser
EDIT: There is a workaround - while the stock browser cannot achieve this, using an embedded WebView gives you a little more control:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
This solved the problem!

How to play Video in Webview via a <iframe> in Android?

I need to play a youtube video in webview, the link to the video is present in my tag. But when I run my application, only a pic(thumbnail) comes in the place with play icon on it, and after clicking on it, it shows a black screen with a seekbar on it at 00:00 time, and loading symbol on it, but then nothing happens or is shown. It than remains there all day.
I am using Emulator with 4.4 Android.
My code is a follows:-
WebView content = (WebView)findViewById(R.id.webView1);
String html="<html><body>Some text goes here<br>"
+ "<iframe src=\"//www.youtube.com/embed/eu9kMIeS0wQ?rel=0\" height=\"360\" width=\"640\" allowfullscreen=\"\" frameborder=\"0\"/></iframe></body></html>";
content.getSettings().setPluginState(PluginState.ON_DEMAND);
content.getSettings().setJavaScriptEnabled(true);
content.getSettings().setAllowFileAccess(true);
content.setWebChromeClient(new WebChromeClient());
content.setWebViewClient(new WebViewClient());
content.loadDataWithBaseURL("SomeStringForBaseURL", html, "text/html", "UTF-8", "");
I had also included the
android:hardwareAccelerated="true"> In the manifest.
DO I need to add any API's for Youtube or for any other thing. or what sort of mistake I am commencing? Any help will be appreciated. Thanks In Advance.
Well everything is fine, and the video is running nice and fast. Well, the emulator requires flash player to be installed, whereas this code makes videos playable on android devices. If anyone needs the source code, they can use the one above for running the videos in WebView via tag on android.

Android webview html5 video autoplay not working on android 4.0.3

I have developed an application where the user can set videos on a web page:
- They can specify a Youtube URL
OR
- They can upload a video
Depending on which option the user chooses i render a video page like this :
If a video is from youtube:
<iframe type="text/html" width="640" height="385" src="http://www.youtube.com/embed/YOUTUBEID?autoplay=1&loop=1&autohide=1&fs=0" frameborder="0"></iframe>
If the video is being uploaded:
<video id="video" style="cursor: pointer;" width="640" height="480" autoplay controls loop>
<source src="../video/Tareas_Diarias_Resumen.mp4" type="video/mp4" />
</video>
OK. All this works perfect on Google Chrome, but the fact is that the video is going to be watched on 16 Samsung GT-P5100 Android 4.03 Tablets.
We created an Android application which embeds an webView like that:
WebView engine = (WebView) findViewById(R.id.web_engine);
engine.setWebChromeClient(new WebChromeClient());
engine.getSettings().setPluginsEnabled(true);
engine.getSettings().setPluginState(PluginState.ON);
engine.getSettings().setJavaScriptEnabled(true);
engine.getSettings().setAllowFileAccess(true);
engine.loadUrl(miUrl);
And here I have two issues:
1- Youtube video. It works ok, I can see the video but with no autoplay, and we want it to work with autoplay. Any solution?
2- The uploaded video does not work, I can see the player but no video.
The video was converted with handbrake, choosing iphone & ipod-touch preset and "Web Optimized" option.
Any help or clue?
Thanks in advance
Auto-play is disabled since Android SDK 17, but you can set setMediaPlaybackRequiresUserGesture to false to re-enable auto-play.
Don't forget to check the SDK version because there is not such function in earlier versions.
int SDK_INT = android.os.Build.VERSION.SDK_INT;
if (SDK_INT > 16) {
engine.getSettings().setMediaPlaybackRequiresUserGesture(false);
}
[upgraded to answer per request]
Autoplay on most mobile platforms (Android, iOS) gets blocked to avoid poor user experiences - video should only play following a user action. You can usually work around it by triggering the play() on another event (eg the onloaded event)
For Android 4.0.x, try triggering the event : loadstart
var vid=document.getElementById('video');
vid.addEventListener("loadstart", showVideo, false);
function showVideo(e) {
vid.play();
}
That can not be achieved, autoplay on most mobile platforms (Android, iOS) gets blocked to avoid poor user experiences. Look at this:
http://www.longtailvideo.com/html5/autoloop/

Categories

Resources