2

I am displaying a youtube link in a webview. I want to autoplay the link. 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 :

myWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
      view.loadUrl(request.getUrl().toString());
      return true;
    }
}
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);

myWebView.loadUrl("https://www.youtube.com/watch?v=YyDnYaFtRS0"); 

Tested on Devices : 1) Amlogic Android Media Boxes(Android 6.0.1) -> Not working. 2) Nexus 5 (Android 6.0) -> Working.

Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
a.prateek
  • 135
  • 4

3 Answers3

1

Try it

myWebView.setWebViewClient(new WebViewClient() {

public void onPageFinished(WebView view, String url{

web.loadUrl("javascript:(function() {

document.getElementsByTagName('video')[0].play(); })()");

}

});

Rashpal Singh
  • 633
  • 3
  • 13
1

I think it's not possible in webview or android browsers. To achieve auto playing, I think you need "YOUTUBE API".

Check below link :

1] There's no way method to do autoplay video on android platform?

2] Youtube Api android autostart

These above links will give you idea about auto playing as well as youtube api. for more detail you can check this link.

Rashpal Singh
  • 633
  • 3
  • 13
1

I am able to solve this problem by using javascript to programatically click on videoElement.

webView.loadUrl("javascript:(function() {"
              + "var videoElem = document.querySelectorAll('video');"
              + "if (videoElem.length > 0) {"
              + "videoElem[0].click(); return;"
              + "} else {"
              + "console.log('Not Found Video Element..!!');"
              + "}"
              + "})()");
a.prateek
  • 135
  • 4