WebView video compatibility for Android versions - android

Following code is used to play embedded youtube videos, on a sample app.
The embedded videos play fine in android version 19, 20 etc., but DO NOT play on device of version 10.
My question is two-fold:
a. How do we find the solution (except asking someone of-course ) ?
Javadocs do not have info. about this.
b. How do we solve this problem ?
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setAllowFileAccess(true);
//webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//return super.shouldOverrideUrlLoading(view, url);
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://youtu.be/7uq-s0dMLsE");
Thanks for all the help in advance !!

Related

Prevent link to be opened in external browser with WebChromeClient

I've developed a small group of .html pages that are stored in a server.
My android app, using a webview with: setWebChromeClient loads these pages.
minSdkVersion 19
targetSdkVersion 24
The problem:
Everytime I need to load a new page, using a link in my .html page, the new page is opened in an external browser.
Url Overriding
I know about the shouldOverrideUrlLoading() method, when we're using the setWebViewClient().
But unfortunately I can't use the normal webview. I need to use the WebChromeClient() because of some feature that only work with this one. (Like the input file)
My doubt is...
How can I override my URL to force them to load inside of the webChromeClient?
I tried this but with no luck:
webView.setWebChromeClient(new WebChromeClient() {
// (...)
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
You can use following code to achieve this.
WebView web = (WebView)findViewById(R.id.web);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportMultipleWindows(true); // This forces ChromeClient enabled.
web.setWebChromeClient(new WebChromeClient(){
#Override
public void onReceivedTitle(WebView view, String title) {
getWindow().setTitle(title); //Set Activity tile to page title.
}
});
web.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
I was using an embedded mobile web application in an Android App making use of the WebChromeClient class and did not want to fiddle around with recompiling the APK.
While looking for the easiest header(); solution (since the mobile web application is in php) I found out that using:
header("Location: url.php", TRUE, 307);
was a quick fix solution without recompiling the Android app.
This allowed the user to re-direct within the app without calling the web browser externally from my app.
Here is the link to the answer by Arindam Nayak where I got the idea from.

My webview video is continously loading, how do I get it to play?

I'm having some trouble trying to get this video playing in my webview. The youtube video appears but whenever I attempt to play the video, all it does is continuously load. How do I fix this? Also, I have the hardware acceleration on true. Here's my current code for playing the video:
String frameVideo = "<html><body>Video From YouTube<br><iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/_4IRMYuE1hI\" frameborder=\"0\" allowfullscreen></iframe></body></html>";
WebView displayYoutubeVideo = (WebView) findViewById(R.id.webView);
displayYoutubeVideo.setWebChromeClient(new WebChromeClient());
displayYoutubeVideo.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = displayYoutubeVideo.getSettings();
webSettings.setJavaScriptEnabled(true);
displayYoutubeVideo.loadData(frameVideo, "text/html", "utf-8");
The youtube video appears but whenever I attempt to play the video,
all it does is continuously load. How do I fix this?
I had the same issue, on a real device (emulator won't work AFAIK). In order to fix that, you need to enable the hardware acceleration for your app. You can enable it by adding:
android:hardwareAccelerated="true"
to your AndroidManifest.xml file. E.g:
<application android:hardwareAccelerated="true" ...>

Trying to autoplay YouTube video in a PhoneGap/Cordova app

I'm developing an app using PhoneGap. So, it's a web app. In this app, I have a requirement to embed a YouTube video and autoplay it whenever the user navigates to that particular page.
I've read that HTML5 video autoplay doesn't work on mobile devices because of bandwidth concers. My question is that is there any way at all to bypass this restriction? I don't mind complex workarounds or hacks that could allow me to do this. Anything at all.
Thanks.
As you pointed out yourself, all autoplay instructions in your code itself will be ignored on load. So we'll implement a function that gets the video in the body and starts playing it.
The following javascript code could do this:
(function() {
document.getElementsByTagName('video')[0].play();
})()
To execute this code after the page has loaded, we need to set a WebViewClient and implement onPageFinished()
webview.setWebViewClient(new CordovaWebViewClient(this, webview) {
// autoplay when finished loading via javascript injection
public void onPageFinished(WebView view, String url) { webview.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); }
});
final WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setPluginState(WebSettings.PluginState.ON);
A full example:
webview = new CordovaWebView(this);
setContentView(webview);
final WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setPluginState(WebSettings.PluginState.ON);
webview.setWebViewClient(new CordovaWebViewClient() {
// autoplay when finished loading via javascript injection
public void onPageFinished(WebView view, String url) { webview.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); }
});
webview.loadUrl("http://html5demos.com/video");
There is a cordova/phonegap plugin that promises to do this. It rely on the Webview method setMediaPlaybackRequiresUserGesture, added in API level 17 (Android 4.2).
It simply call this method on the webview:
WebView view = getWebViewFromPlugin();
view.getSettings().setMediaPlaybackRequiresUserGesture(false);
It's also available in phongap build.

How to run the HTML5 video in webview in android

From last one week i try to find how to play a html5 video in android webview i read so many blog and questions in the stack overflow but my problem is solve.i am using .mp4 , .wevm ,ogv and also .3gp format but noon of format is running.
i run these videos in the two different device one is micro-max funbook with android 4.0 and the another device is a Samsung device with 2.3.3 android os.
this is my code.
url="file://"+Environment.getExternalStorageDirectory()+"/unzipped/HTML5/res/test1.html";
webview = (WebView) findViewById(R.id.webview);
webview.setWebChromeClient(new WebChromeClient());
webview.getSettings().setPluginsEnabled(true);
webview.getSettings().setPluginState(PluginState.ON);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webview.loadUrl(url);
For API level 11+ it should suffice that you add the attribute android:hardwareAccelerated="true" to the <application> tag in your Android manifest file.
For older APIs you need to implement the methods onShowCustomView and onHideCustomView in your your own WebChromeClient.
Or you can check out the html5webview

Add javascript into WebView

I am developing an application where in 1 part I want to add javascript into WebView.. But am not getting how to do it in an appropriate way.. Can anyone pls guide me into this?????
I am doing it like:
wb=(WebView)findViewById(R.id.webView1);
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setPluginState(WebSettings.PluginState.ON);
wb.getSettings().setPluginsEnabled(true);
wb.loadUrl("javascript:<script " ></script> ");
wb.setWebViewClient(new HelloWebViewClient());
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
It was very simple..also without using any javascriptInterface..In my code, Instead writing:
wb.loadUrl("javascript:<script> </script>");
use,
wb.loadDataWithBaseURL(null,"<script> </script>","text/html","utf-8",null);
and its working now :)
Look at these nice tutorials about how to implements javascript in webview in android..
I think its provides you all the information what you needed..
Android WebView
Android WebView, Javascript and CSS
EDIT: Further if your implemented code having any exception or not working then please post that code and exception then here we can help you..

Categories

Resources