Android: How to force WebView to play a video from youtube? - android

i want to play a youtube video using embeded url in webview in my app but the video is opened automatically in youtube application that is insatlled in my phone instead of played in webview in the activity
here is my code
WebView mWebview = (WebView) findViewById(R.id.mwebview);
mWebview.getSettings().setJavaScriptEnabled(true);
mWebview.getSettings().setBuiltInZoomControls(true);
mWebview.getSettings().setSupportZoom(false);
mWebview.loadUrl("http://www.youtube.com/embed/" + video.youtube_id);

i find the answer
mWebview = (WebView) findViewById(R.id.mwebview);
mWebview.setInitialScale(1);
mWebview.getSettings().setPluginState(WebSettings.PluginState.ON);
mWebview.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setAllowContentAccess(true);
webSettings.setEnableSmoothTransition(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(false);
webSettings.setUseWideViewPort(true);
webSettings.setAppCacheEnabled(true);
webSettings.setSupportMultipleWindows(true);
mWebview.loadUrl("http://www.youtube.com/embed/" + video.youtube_id);

Related

web view don't load when language change but in chrome it does reload

i am making a web view app where there is a language tabs on header like spinner when i change language in chrome it do refresh and language changes but in web view it don't detect language change on same link and don't restart until manually restart i have enabled java script and sharing some code i search every where but didn't get any solution. and the languages load at same time when page load.
mWebView = (AdvancedWebView) findViewById(R.id.webView);
mWebView.loadDataWithBaseURL(null,URL,"text/html", "utf-8", null);
mWebView.setWebViewClient(new WebViewClient());
mWebView.setCookiesEnabled(true);
mWebView.setDesktopMode(true);
mWebView.setThirdPartyCookiesEnabled(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setSaveFormData(true);
mWebView.getSettings().setAllowContentAccess(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setAllowFileAccessFromFileURLs(true);
mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.setClickable(true);
mWebView.setWebChromeClient(new WebChromeClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);
webSettings.setDefaultTextEncodingName("utf-8");

android webview show credentials popup

Here is the link where I want to login:
http://audidome-dev.webspaces.vektorrausch.net/app/login.php
and in normal browser it looks so
but I can't get this popup in my WebView.
Here is my code:
mWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setDomStorageEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setVisibility(View.VISIBLE);
mWebView.loadUrl("http://audidome-dev.webspaces.vektorrausch.net/app/login.php");
WebView won't display this pop-up. It will ask you for the credentials and it's up to you to fill the credentials (display the pop-up).

Android - can't seem to enable JavaScript on a WebView?

Is there a way to enable JavaScript in a WebView?
I am currently trying to enable JavaScript by doing this:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
webview = new WebView(this);
setContentView(webview);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(1);
webview.getSettings().setPluginState(PluginState.ON);
WebSettings webSettings = webview.getSettings();
webSettings.setLoadsImagesAutomatically(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setUseWideViewPort(true);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
webview.setWebChromeClient(new WebChromeClient(){});
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
webSettings.setDatabaseEnabled(true);
webSettings.setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");
webview.loadUrl("http://www.youtube.com/user/Okudjavavich");
}
But JS seems to not be enabled because the YouTube videos do not play YouTube ads. Would anyone know what may be the problem?
Is there a way to enable JavaScript in a WebView?
Yes, via the code that you have:
webview.getSettings().setJavaScriptEnabled(true);
In your case, the problem was that your test (playing YouTube videos and looking for ads) was complicated. A simpler test helped determine that JavaScript indeed was enabled, but that something else was influencing Google's YouTube page-serving. In this case, it appears to be based on user agent.

Android WebView - code that can display and play a podcast can't play a YouTube video

I have this code which works to display a WebView and use it:
WebView webview = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
//setTheme(R.style.Theme_Sherlock_Light);
super.onCreate(savedInstanceState);
//setContentView(R.layout.podcasts);
webview = new WebView(this);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(1);
webview.getSettings().setPluginState(PluginState.ON);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
//webSettings.setBuiltInZoomControls(true);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
//webSettings.getMediaPlaybackRequiresUserGesture();
webSettings.setAllowContentAccess(true);
webSettings.setEnableSmoothTransition(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(true);
webSettings.setUseWideViewPort(true);
setContentView(webview);
webview.loadUrl("url_to_go_to");
But when I try to use this code to point to a YouTube channel, it just shows a blank screen.
Any idea why that would happen?
Thanks!
In order to get HTML5 videos to show up you need to enable the following things in the WebView:
WebView view;
... //initialize WebView
WebSettings webViewSettings = view.getSettings();
view.setWebChromeClient(new WebChromeClient(){}); //just added this
webViewSettings.setDomStorageEnabled(true);
webViewSettings.setAppCacheEnabled(true);
webViewSettings.setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
webViewSettings.setDatabaseEnabled(true);
webViewSettings.setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");
You also need to put android:hardwareAccelerated="true" in your AndroidManifest. Otherwise, HTML5 stuff like YouTube videos won't work.

Android WebView - when trying to play a YouTube video, screen is black but audio plays

I am trying to play a YouTube channel from a WebView. It feels like I almost got it but I have two problems. Because there is some redirect when trying to load the YouTube channel url, the shouldOverrideUrlLoading method seems to be breaking things and I just get a gray screen.
And the other problem is that if I comment out that line and the Channel loads, when I play the videos, the video seems to load, and the audio plays, but the screen of the youtube stays black.
Here is my code
public class YoutubeActivity extends BaseActivity
{
WebView webview = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
webview = new WebView(this);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(1);
webview.getSettings().setPluginState(PluginState.ON);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
WebSettings webSettings = webview.getSettings();
webSettings.setEnableSmoothTransition(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setAllowContentAccess(true);
webSettings.setSupportZoom(true);
webSettings.setUseWideViewPort(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
webSettings.setDatabaseEnabled(true);
webSettings.setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");
webview.loadUrl("http://www.youtube.com/g33ktalktv");
Would anyone know how to fix this?
Thank you!

Categories

Resources