How to auto play iframe youtube on webview android

2020-04-07 04:12发布

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=1" to videoUrl but nothing change it's still not auto play.

public class LandVideoAct extends Activity {
    WebView mWebView;
    String videoURL = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.landfull);
        videoURL = "http://www.youtube.com/embed/VYIi49czywo?autoplay=1";
        mWebView = (WebView) findViewById(R.id.fullwebview);

        String vid = "<html><body style=\"margin: 0; padding: 0\"><iframe  width=\"100%\" height=\"100%\" src=\""+videoURL+"\" type=\"text/html\" frameborder=\"0\"></iframe><body><html>";

        WebChromeClient mWebChromeClient = new WebChromeClient(){
            public void onProgressChanged(WebView view, int newProgress) {
            }
        };

        mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
        mWebView.setWebChromeClient(mWebChromeClient);
        mWebView.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                mWebView.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); 
            }
        });
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setAppCacheEnabled(true);
        mWebView.setInitialScale(1);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setUseWideViewPort(true);
        if (Build.VERSION.SDK_INT < 17) {
            Log.i("GPSNETWORK", "<17");
        } else {
            Log.i("GPSNETWORK", Build.VERSION.SDK_INT+">=17");
            mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
        }
        mWebView.loadData(vid, "text/html", "UTF-8");
    }
}

7条回答
▲ chillily
2楼-- · 2020-04-07 04:32

I think its 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.

查看更多
甜甜的少女心
3楼-- · 2020-04-07 04:35

It's disabled by default based on operating system reason. Chrome disables content by default, too. You can try to code your own app or use Java, Javascript and the YouTube-API instead. Maybe webkit will let you allow to decide what to play based on your battery state in the future.

查看更多
闹够了就滚
4楼-- · 2020-04-07 04:41

Use Desktop mode on WebView. Youtube will autoplay. Add this:

webView.getSettings().setUserAgentString("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.137 Safari/537.36")

Like this:

val webView: WebView = findViewById(R.id.webView)
webView.settings.javaScriptEnabled = true
webView.settings.domStorageEnabled = true
webView.loadUrl("http://your.web.site/")
webView.webChromeClient = object : WebChromeClient() {}
webView.settings.setPluginState(WebSettings.PluginState.ON)
webView.settings.setPluginState(WebSettings.PluginState.ON_DEMAND)
webView.settings.setUserAgentString("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.137 Safari/537.36")
if(android.os.Build.VERSION.SDK_INT>16){webView.settings.setMediaPlaybackRequiresUserGesture(false)}
webView.webViewClient = object : WebViewClient() {
    override fun onPageFinished(webView: WebView, url: String) {
        super.onPageFinished(webView, url)
    }
}
查看更多
该账号已被封号
5楼-- · 2020-04-07 04:46

Do like this. Hope it helps

String html = "<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 95%; padding:0px; margin:0px\" id=\"ytplayer\" type=\"text/html\" src=\"http://www.youtube.com/embed/"
                + "J2fB5XWj6IE?autoplay=1"
                + "&fs=0\" frameborder=\"0\">\n"
                + "</iframe>\n";

From below link

In Chrome, autoplay is also disabled

(https://code.google.com/p/chromium/issues/detail?id=159336)

I believe the same happens with the recent versions of the default Android Web Browser.

查看更多
Deceive 欺骗
6楼-- · 2020-04-07 04:49

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");
}
查看更多
成全新的幸福
7楼-- · 2020-04-07 04:51

This code will do the trick, I'm using jquery, but it can be done with plain javascript.

<iframe id="myvideo" src="https://www.youtube-nocookie.com/embed/Vhh_GeBPOhs?autoplay=1&amp;rel=0&amp;controls=0&amp;showinfo=0&amp;rel=0&amp;loop=1&amp;modestbranding=1&amp;wmode=transparent&amp;mute=1amp;enablejsapi=1" width="560" height="315" frameborder="0" allowfullscreen></iframe>
<script>
setInterval(function(){
  $('#myvideo')[0].contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');
 }, 250);
</script>
查看更多
登录 后发表回答