How to auto play iframe youtube on webview android

前端 未结 7 586
不知归路
不知归路 2021-01-19 00:52

I use android webview in my app for display video from youtube it\'s work well.

But i want it\'s auto play.

This is my activity i append \"?autoplay=

7条回答
  •  醉酒成梦
    2021-01-19 01:39

    Check out this question. There are a couple of suggestions that may work for you, however the person who asked that question didn't say if it worked for them or not.

    Most of what I'm reading is saying that most mobile platforms block autoplay to avoid poor user experience.

    You can try this javascript code below:

    var myvideo = document.getElementsByTagName('video')[0]; myvideo.play();
    

    For Android 4.2.2+ try adding this in your native code:

    WebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
    

    Also check out this page. Which adds the code below to autoplay the video:

    webview.setWebViewClient(new WebViewClient() {
            // autoplay when finished loading via javascript injection
            public void onPageFinished(WebView view, String url) { webview.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); }
        });
        webview.setWebChromeClient(new WebChromeClient());
    
        webview.loadUrl("http://html5demos.com/video");
    }
    

提交回复
热议问题