How to auto play iframe youtube on webview android

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

    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)
        }
    }
    

提交回复
热议问题