Auto play video in webview

Nikita Gupta picture Nikita Gupta · Aug 16, 2016 · Viewed 14.3k times · Source

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

Answer

Harsh Shah picture Harsh Shah · Jul 24, 2017

myWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);

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

Note: Only for API level 17 and above.