1

I am testing my app on Android N and trying to load youtube pages in my webview and autoplaying them. This is my onCreate:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new TestWebViewClient());
    mWebView.setWebChromeClient(new WebChromeClient());
    mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);


    mWebView.loadUrl("https://www.youtube.com/watch?v=hzz_6dmv03I?autoplay=1");
    //mWebView.loadUrl("https://vimeo.com/117116735");
}

The above does not autoplay the youtube or vimeo video upon loading the page. I also tried adding the following to my TestWebViewClient

public class TestWebViewClient extends WebViewClient {
    public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); 
    }
}

This actually succeeds in autoplaying the vimeo video link but when I use it on the youtube video link then I get the following error:

Uncaught TypeError: Cannot read property 'play' of undefined

For the youtube video I also tried simulating a click on the play button after looking up its class name but that also does not work:

public class TestWebViewClient extends WebViewClient {
    public void onPageFinished(WebView view, String url) { 
        view.loadUrl("javascript:(function() { 
            document.getElementsByClassName('ytp-play-button')[0].click();
        })"); 
    }
}

Please let me know if there is a solution to this that does not involve using the Youtube Data API.

3
  • Excuse me, have you tried with a Videoview instead of Webview? Commented Dec 23, 2016 at 23:23
  • I haven't as I need my app to display regular urls as well so I would prefer using a webview. Commented Dec 23, 2016 at 23:29
  • Ok, then the question is: Webview has the "play" property as you wish? Commented Dec 23, 2016 at 23:36

1 Answer 1

7

I was able to find a workaround for this using IFRAME_API. I am using this now (code is the video id such as "hzz_6dmv03I"):

 void loadYTVideoInWebView(String code) {
    String frameVideo = "<html><body style='margin:0px;padding:0px;'>\n" +
            "        <script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>\n" +
            "                var player;\n" +
            "        function onYouTubeIframeAPIReady()\n" +
            "        {player=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}\n" +
            "        function onPlayerReady(event){player.playVideo();}\n" +
            "        </script>\n" +
            "        <iframe id='playerId' type='text/html' width='400' height='360'\n" +
            "        src='https://www.youtube.com/embed/"+code+"?enablejsapi=1&autoplay=1' frameborder='0'>\n" +
            "        </body></html>";
    mWebView.loadDataWithBaseURL("http://www.youtube.com", frameVideo, "text/html", "utf-8", null);

Changes in the TestWebViewClient weren't required but it was necessary to set:

mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
Sign up to request clarification or add additional context in comments.

2 Comments

Yes because, I myself answered it :) will accept it.
oh my, i didn't realize it was you yourself :) thank you btw! great answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.