Unable to disable autoplay for video from a URL in WebView - android

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);
}

Related

Webview navigation returning wrong url

I have a webview that looks like this
WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl("https://myurl.com/");
webview.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Toast.makeText(LoginActivityWebView.this, url, Toast.LENGTH_SHORT).show();
return false;
}
}
When i change my URL to google everything works great, i can navigate to different sites and everything works.
When I put in my URl it is supposed to link to myurl2, but when I click it the toast prints myurl instead and the page juts reloads (not helpful!).
Any idea as to how this could happen and where shouldOverrideUrlLoading gets its url from?
If i load myurl in a browser and the user clicks a button then it works correctly.
If i load myurl in a webview, but open a browser on links pressed, then when the link is pressed it opens myurl on in a broswer and if you click the button again it correctly opens myurl2 (super weird).
Any ideas?
If the URL is opening in Chrome properly, you should consider adding, webView.getSettings().setJavaScriptEnabled(true) to your code and try again.
You can get the loaded URL from
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});

wynk OR Gaana music not playing on webview android

I want to play a music from any music app(gaana/wynk/saavn etc) using webview android. My requirement is I want to open a music app in webview and user can play music directly using webview just like we open (gaaan/wynk/saavn) in web browser of mobile device and you can play any song.
I tried gaana and wynk music, both are able to open on webview but when i am clicking on any songs that songs does not play. do i need to enable any settings for that i already have enabled javascript.
below is the code i have tried.
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient()
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
}
});
Please suggest any solution for this.
its because app cant use storage.
WebView webView = (WebView) findViewById(R.id.webview);
webview.getSettings().setDomStorageEnabled(true);
This solves the problem !

Auto play video in webview

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.

Loading pdf or url in Android Webview

I am working on an Android application that opens urls its content is HTML or Pdf ... I used this way to open it
WebView webView = (WebView) mContentView.findViewById(R.id.web_view);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
// This is the url that may be .html or .pdf
String url = "https://uva.onlinejudge.org/external/1/119.html";
//String url = "https://uva.onlinejudge.org/external/11/1124.html";
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progress >= 100) {
setContentShown(true);
}
}
});
webView.loadUrl(url);
the first url opens normally in the webview, but the second url doesn't show anything in the webview .. and If I try to open it on the browser it redirects me to this url [https://uva.onlinejudge.org/external/11/p1124.pdf] and open normally.
Any help will be appreciated.
Set a download listener to your webview. Inside the listener, you can choose to download the PDF by overriding its methods.
webView.setDownloadListener(new (DownloadListener listener) { // is it a pdf? });

How to webview return to the last url after playing video?

there is a webview with video links ,after finish playing a video in media player,the media player close and returns to webview ,but webview loads its original url instead of the url that I click the video link,I wander how to return to the last url after the media player close?
thx very much!
You can detect the url changes in your webview, setWebViewClient method of webview and then implement a new webViewClient, then you can access full url by using onPageFinished method. see code below.
webView.setWebViewClient(new WebViewClient()
{
#Override
public void onPageFinished(WebView view, String url)
{
//save url in a static variable so that this will ensure it will be the last url that will be loaded
}
});

Categories

Resources