Android Webview Youtube Embed Video Autoplay Not Working
Solution 1:
Inspired by Orsi's answer, I was able to mimic onClick() event on the center of the WebView that showed video player. This ultimately played the video automatically, or rather, without user's interaction.
privateclassAutoPlayVideoWebViewClientextendsWebViewClient {
@OverridepublicvoidonPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// mimic onClick() event on the center of the WebViewlongdelta=100;
longdownTime= SystemClock.uptimeMillis();
floatx= view.getLeft() + (view.getWidth()/2);
floaty= view.getTop() + (view.getHeight()/2);
MotionEventtapDownEvent= MotionEvent.obtain(downTime, downTime + delta, MotionEvent.ACTION_DOWN, x, y, 0);
tapDownEvent.setSource(InputDevice.SOURCE_CLASS_POINTER);
MotionEventtapUpEvent= MotionEvent.obtain(downTime, downTime + delta + 2, MotionEvent.ACTION_UP, x, y, 0);
tapUpEvent.setSource(InputDevice.SOURCE_CLASS_POINTER);
view.dispatchTouchEvent(tapDownEvent);
view.dispatchTouchEvent(tapUpEvent);
}
}
Somewhere,
myWebView.setWebViewClient(newAutoPlayVideoWebViewClient());
Solution 2:
First used below code :
webSettings.setMediaPlaybackRequiresUserGesture(false);
After that used below method for loading:
webView.loadDataWithBaseURL("https://www.youtube.com/", HTML.replace("CUSTOM_ID","YOUR_YOUTUBE_VIDEO_ID"), "text/html", "utf-8", null);
In HTML string pass below Code with YouTube API (Replaced your YouTube video ID)
<!DOCTYPE html><html><styletype="text/css">html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
background-color: #000000;
overflow: hidden;
position: fixed;
}
</style><body><divid="player"></div><script>var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
functiononYouTubePlayerAPIReady() {
player = newYT.Player('player', {
height: '100%',
width: '100%',
videoId: 'CUSTOM_ID',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
},
playerVars: {
'autoplay': 0,
'showinfo': 1,
'controls': 1
}
});
}
functiononPlayerReady(event) {
event.target.playVideo();
}
var done = false;
functiononPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
done = true;
}
}
functionstopVideo() {
player.stopVideo();
}
</script></body></html>
Solution 3:
Finally I made it.. You need to use this API to play the video on "OnReady" event: https://developers.google.com/youtube/iframe_api_reference#Getting_Started
Load the page that contains the code from this example.
And make sure you have
webSettings.setMediaPlaybackRequiresUserGesture(false);
(API 16+)
Solution 4:
You can't do autoplay with JavaScript on the webview. In order to enhance user experience , WebKit disables the option to autoplay videos on mobile browsers.
Solution 5:
The autoplay issue is a known problem, please take a look to my answer here.
Post a Comment for "Android Webview Youtube Embed Video Autoplay Not Working"