Auto play video in webview
Asked Answered
T

5

11

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");  
Tragedian answered 16/8, 2016 at 12:41 Comment(0)
M
32

myWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);

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

Note: Only for API level 17 and above.

Mckinleymckinney answered 24/7, 2017 at 14:28 Comment(2)
I don't know if it makes to enable video auto play in all case but perfect me. Thank you very much.Flittermouse
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.Sixtyfour
B
17

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.

Bornholm answered 7/5, 2018 at 7:20 Comment(0)
K
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();
}
Knowall answered 22/8, 2017 at 9:52 Comment(0)
R
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

Rempe answered 1/12, 2016 at 19:49 Comment(3)
it gives me an error in console: Uncaught TypeError: Cannot read property 'play' of undefined", source:Vengeful
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 <video></video> Tag in HTML.Rempe
@AliNoureddine , my video is inside <video> tag but still I am getting that error!Snafu
D
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.
Differentiate answered 14/12, 2021 at 3:41 Comment(1)
where's the references?Wrought

© 2022 - 2024 — McMap. All rights reserved.