i am not able to autoplay my video please help in this. my sdk version
android:minSdkVersion="14"
android:targetSdkVersion="19" />
i tried to put java script as specifed in code:
public void onPageFinished(WebView view, String url) { webView.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); }
});
i also try to append autoplay in URL but not working
//webView.loadUrl("http://youtube.com/embed/oY2OxMpCUVY?autoplay=1");
my web settings `
customViewContainer = (FrameLayout)rootView.findViewById(R.id.customViewContainer);
webView = (WebView) rootView.findViewById(R.id.HelpView_Video);
final GlobleClass globalVariable = (GlobleClass) GlobleClass.getContext();
mWebViewClient = new HelpWebViewClient();
webView.setWebViewClient(mWebViewClient);
mWebChromeClient = new myWebChromeClient();
webView.setWebChromeClient(mWebChromeClient);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(PluginState.ON);
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(); })()"); }
});
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
// webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
// }
webView.getSettings().setAppCacheEnabled(true);
//webView.getSettings().setBuiltInZoomControls(true);
// webView.getSettings().setSaveFormData(true);
//webView.loadUrl("http://youtube.com/embed/oY2OxMpCUVY?autoplay=1");
webView.loadUrl(globalVariable.getHelpVideoUrl());
`
To make an embedded video autoplay, add "&autoplay=1" to the video's embed code right after the video ID (the series of letters that follows "embed/"). Embedded videos that are autoplayed don't increment video views.
The Android system webview custom cache file has been deprecated and removed in Android 13. New apps and any app updates will now use the operating system default cache location.
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.
private class AutoPlayVideoWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// mimic onClick() event on the center of the WebView
long delta = 100;
long downTime = SystemClock.uptimeMillis();
float x = view.getLeft() + (view.getWidth()/2);
float y = view.getTop() + (view.getHeight()/2);
MotionEvent tapDownEvent = MotionEvent.obtain(downTime, downTime + delta, MotionEvent.ACTION_DOWN, x, y, 0);
tapDownEvent.setSource(InputDevice.SOURCE_CLASS_POINTER);
MotionEvent tapUpEvent = 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(new AutoPlayVideoWebViewClient());
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>
<style type="text/css">
html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
background-color: #000000;
overflow: hidden;
position: fixed;
}
</style>
<body>
<div id="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;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '100%',
width: '100%',
videoId: 'CUSTOM_ID',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
},
playerVars: {
'autoplay': 0,
'showinfo': 1,
'controls': 1
}
});
}
function onPlayerReady(event) {
event.target.playVideo();
}
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With