9

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");  
Nikita Gupta
  • 93
  • 1
  • 1
  • 5

5 Answers5

29

myWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);

https://developer.android.com/reference/android/webkit/WebSettings.html#setMediaPlaybackRequiresUserGesture(boolean)

Note: Only for API level 17 and above.

Harsh Shah
  • 301
  • 3
  • 3
  • I don't know if it makes to enable video auto play in all case but perfect me. Thank you very much. – bb14816 Feb 06 '18 at 06:35
  • This works, but I had to manually specify this in JS too: `document.getElementById("video").muted = "true";` just setting `muted` in HTML didn't work for me. if you just take this code make sure you match the HTML ID of your video. – Yigal Nov 15 '19 at 09:31
12

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.

ColdFire
  • 6,764
  • 6
  • 35
  • 51
Priyanka G
  • 141
  • 2
  • 6
3

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();
}
XuLu
  • 79
  • 5
2
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

Ahmad Tn
  • 360
  • 3
  • 16
  • it gives me an error in console: Uncaught TypeError: Cannot read property 'play' of undefined", source: – Ali Noureddine Apr 02 '18 at 12:10
  • 1
    The error given implies that the JavaScript code `document.getElementsByTagName('video')` is returning an empty list, meaning that your video is not placed inside a `` Tag in HTML. – Ahmad Tn Apr 03 '18 at 14:16
1

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.