How to autoplay youtube videos in Android WebChromeClient - android

I have created a simple APK for Android that uses WebChromeClient
mWebChromeClient = new myWebChromeClient();
webView.setWebChromeClient(mWebChromeClient);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSaveFormData(true);
webView.loadUrl("http://example.com/embed.php");
I have set the user agent to desktop browser
String ua = "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
webView.getSettings().setUserAgentString(ua);
I added hardware=accelerated to the manifest
android:hardwareAccelerated="true"
Still, I am unable to get the youtube autoplay to work.
One clue is when I go to this URL in my app https://www.youtube.com/html5 it states that "The default player is currently used"
How can I get the embedded youtube video to autoplay in my WebChromeClient?
I have control over both the APK and the html page where the youtube video is embeded, so a solution for either end would work for me!

Related

YouTube thumbnail won't load in webview

We have a WebView based android app which loads webpage consisting <a> tags linking to YouTube video.
The web app works fine in a browsers like Chrome for Android and loads thumbnail from YouTube as expected but within WebView the <a> tag won't load the thumbnail of YouTube video.
How can we force the WebView to behave similar to Chrome browser
Have tried setting clients like below, which didn't help
webview.setWebViewClient(new WebViewClient());
webview.setWebChromeClient(new WebChromeClient());
Even added hardware acceleration in AndroidManifest.xml but it didn't work either
android:hardwareAccelerated="true"
Note: JavaScript is also enabled.
Did you try to enable javascript in your webview?
You can enable it like this:
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

Difference between Chrome Custom Tabs and WebView

I'm using a webview to open a website. The problem I'm getting is after user clicks on a button, it always get stuck to the Loading dialog and doesn't show the expected results. But in Google Chrome or custom tabs, it works perfectly fine. I'm not overriding onPageFinished(), onPageStarted() or any other webview method, they are as it is. These are my webview settings:
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.getSettings().setSupportMultipleWindows(true);
WebSettings settings = webView.getSettings();
settings.setDomStorageEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
String newUA= "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0";
webView.getSettings().setUserAgentString(newUA);
webView.clearFormData();
webView.getSettings().setSavePassword(false);
webView.getSettings().setSaveFormData(false);
webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(true);
Just wanted to know what settings could be different in custom tabs and my webview that's causing the issue. And I think it's probably not possible but still asking is there any way to imitate the browser settings of custom tabs for the webview so that I can check if it works that way

Iframe video embedded link not working on few android devices

I have an Iframe link :
<div style="position:relative; padding-bottom:56.25%; overflow:hidden;"><iframe src="html link here" width="100%" height="100%" frameborder="0" scrolling="auto" allowfullscreen style="position:absolute;"></iframe></div>
I am loading this in a webview. This works fine. But the problem is in some devices it shows only the player, video is not playing. What could be the issue ? Should I add any additional settings for the webview ?
I searched for the issue, I found out that few devices won't support flash player so the video won't play. If this is the issue how can I fix this?
Below is the webview code :
mWebViewClient = new myWebViewClient();
webView.setWebViewClient(mWebViewClient);
mWebChromeClient = new myWebChromeClient();
webView.setWebChromeClient(mWebChromeClient);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setSaveFormData(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.loadDataWithBaseURL(baseUrl,data_html,"text/html","utf-8",historyUrl);
It's 2017, and FlashPlayer was been deprecated by Android since 2012.
I strongly recommend you NOT to look at any other FlashPlayer solution, because the amount of Android device w/o FlashPlayer support will only grow up.
Look for a HTML5 player instead......
To rectify this issue you can use an HTML5 player which has the following structure <iframe src="https://www.yourDomain.com/embed/VIDEO_ID"></iframe>

In Android, why does a webpage display correctly on Google Chrome and not in a WebView?

My webpage works fine on Google Chrome on my Android phone, but it does not display well when I load it inside a WebView.
My WebView settings as follows:
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setDatabaseEnabled(true);
webview.getSettings().setAppCacheEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
What could be the problem?
Try this before the settings but after you put in the URL
nameOfWebView.setDrawingCacheEnabled(true);
nameOfWebView.buildDrawingCache();
webview.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);

Android Webview youtube video open full screen

I am developing an android app.This app has a webview. It contains several youtube iframes. When I click the video, it is opened in the webview. But I want to open the video in new full screen or native video player.
This is code snippet
webView = (WebView) findViewById(R.id.webView);
mWebChromeClient = new myWebChromeClient();
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSaveFormData(true);
webView.loadUrl("http://m.youtube.com");
But When I open m.youtube.com from chrome and when I click any video, The video is being opened in new video screen. How can I do the same like chrome.
I have been googled and read all other problem in stackoverflow.com I haven't get any successfull result.
Please help.
Thank you.

Categories

Resources