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=
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.
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)
}
}
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(); })()");
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&rel=0&controls=0&showinfo=0&rel=0&loop=1&modestbranding=1&wmode=transparent&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>
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");
}
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.