I have an Android app that plays embedded html5 videos. The html code that I receive does not include the "controls" attribute in the video tag. I'm wondering if there is a way to force the controls to be shown while the video is in the WebView. In 2.3 you can use WebChromeClient to display the full video. However in 4.x it appears you need to show the full controls first in order to take advantage of this class.
You could add the controls attribute:
webView.getSettings().setJavascriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView webView, String url) {
super.onPageFinished(webView, url);
webView.loadUrl(
"javascript:(function() {" +
"var video = document.getElementsByTagName('video')[0];" +
"video.controls = 'controls';" +
"})()"
);
}
});
Related
I have webview displayed on custom dialog, I am loading an URL to WebView which is video URL, by default the video is loaded and starts playing, I want to disable auto play function. Here is my code for WebView
wvActiveVideo.setWebViewClient(new WebViewClient());
wvActiveVideo.getSettings().setJavaScriptEnabled(true);
wvActiveVideo.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wvActiveVideo.getSettings().setPluginState(WebSettings.PluginState.ON);
wvActiveVideo.getSettings().setMediaPlaybackRequiresUserGesture(false);
wvActiveVideo.setWebChromeClient(new WebChromeClient());
wvActiveVideo.setWebViewClient(new WebViewClient());
wvActiveVideo.getSettings().setSupportZoom(true);
wvActiveVideo.getSettings().setBuiltInZoomControls(true);
wvActiveVideo.getSettings().setDisplayZoomControls(false);
String url = model.getVideo();
wvActiveVideo.loadUrl(model.getVideo());
It depends of your algorithm.
First advice , before load url show your user play button background.
Second advice is data manupulation ;
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:document.getElementById(\"video-test\").innerHTML = \"\";void(0);");
webView.setVisibility(View.VISIBLE);
}
I am new in android and I am displaying a news link in a webview. News link contains a video. Problem is that, After opening a link i have to click on video then video is playing but i want that video should be play automatically.
Thanks in advance.
My code is:
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setPluginState(PluginState.ON);
myWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) { web.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); }
});
myWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.loadUrl("http://aajtak.intoday.in/livetv.html");
myWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
https://developer.android.com/reference/android/webkit/WebSettings.html#setMediaPlaybackRequiresUserGesture(boolean)
Note: Only for API level 17 and above.
This works for me.
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
using above, after opening a link no need to click on video, the video is playing automatically.
video on webview didn't support 'autoplay'.
so we shoud start video by hand:
android:
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:onPageFinished();");
}
JS:
function onPageFinished() {
var video = document.getElementById("video");
video.play();
}
myWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {web.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); }
});
You should implement loadUrl on the WebView...
Simply replace web.loadUrl with view.loadUrl and it should work just fine
Just make your webview thinking he is running in PC instead mobile
Insert this os MainActivity file
webView.getSettings().setUserAgentString("1");//for desktop 1 or mobile 0.
I am using webview in my android app. I am using ActionBar to show the title. So I want to remove title of HTML page shown in webview. So how can I do that? Thanks.
You can do something like this.
web.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url){
web.loadUrl("javascript:document.getElementById('pagelet_bluebar')[0].style.display=\"none\";");
}
})
Or go for Jsoup
Actually, my HTML page having tag as:
<div class="titlebar">Terms and Conditions</div>
So, following code has solved my problem using getElementsByClassName:
browser.loadUrl("javascript:(function() { " +
"document.getElementsByClassName('titlebar')[0].style.display=\"none\"; " +
"})()");
I build a little Android library in Eclipse, that open new Activity, put inside it WebView and load into WebView html page with video. I'm using WebView like:
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
showContentOrLoadingIndicator(true);
}
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
and
webView.loadUrl("http://www.example.com/mobile/player/" + VeediUtils.GAME_ID + "/" + ((JSONObject)VeediUtils.LEVELS.get(gameLevel - 1)).getInt("unique_id") + ".html" + ((debugState == 0) ? ("") : ("?debug=veedi")));
Next step, I had built new project, added my first project like a Android dependency, and all works ok, how it should. But when I gave my library to other developer (I don't have access to his code) and he added it like Android dependency to his project, he found an issue: there is he hear sound but instead of video he see black screen. So here the question: what it can be in his application, that break my video in WebView? Thanks.
The webview does not properly support HTML5 video.
See here for a library for proper video support.
If the video is in flash format, it will not work on android 4.4 and up. Otherwise, the user needs to have flashplayer installed, and you need to enable plugins in your webview.
Some websites have comments portion and many other unnecessary portions. I want to just display the main article excluding the other portions in my custom Android WebView.
I have tried the example from Display a part of the webpage on the webview android
But it still shows the full website.
There is also the loaddatawithbaseurl class in Android but it is not working too as you can't specify till which portion of a website to show using this class.
Is there any other way to do this?
Here it is with some changes...
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
String javaScript = "javascript: var form = document.getElementsByClassName('university_corner');"
+ "var body = document.getElementsByTagName('div');"
+ "body[0].innerHTML = form[0].innerHTML";
webView.loadUrl(javaScript);
}
});
webView.loadUrl(url);