How to auto play iframe youtube on webview android

前端 未结 7 503
不知归路
不知归路 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:33

    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.

    0 讨论(0)
  • 2021-01-19 01:33

    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)
        }
    }
    
    0 讨论(0)
  • 2021-01-19 01:33

    A little late but I hope this helps.

    After your page is finished loading run this:-

     yourWebView.loadUrl("javascript:(function() { document.getElementsByClassName('ytp-large-play-button ytp-button')[0].click(); })()");
    
    0 讨论(0)
  • 2021-01-19 01:37

    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>
    
    0 讨论(0)
  • 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");
    }
    
    0 讨论(0)
  • 2021-01-19 01:45

    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.

    0 讨论(0)
提交回复
热议问题