How to auto play iframe youtube on webview android
Solution 1
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.
Solution 2
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");
}
Related videos on Youtube
user2955394
Updated on March 30, 2020Comments
-
user2955394 about 4 years
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=1" to videoUrl but nothing change it's still not auto play.
public class LandVideoAct extends Activity { WebView mWebView; String videoURL = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.landfull); videoURL = "http://www.youtube.com/embed/VYIi49czywo?autoplay=1"; mWebView = (WebView) findViewById(R.id.fullwebview); String vid = "<html><body style=\"margin: 0; padding: 0\"><iframe width=\"100%\" height=\"100%\" src=\""+videoURL+"\" type=\"text/html\" frameborder=\"0\"></iframe><body><html>"; WebChromeClient mWebChromeClient = new WebChromeClient(){ public void onProgressChanged(WebView view, int newProgress) { } }; mWebView.getSettings().setPluginState(WebSettings.PluginState.ON); mWebView.setWebChromeClient(mWebChromeClient); mWebView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { mWebView.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); } }); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setAppCacheEnabled(true); mWebView.setInitialScale(1); mWebView.getSettings().setLoadWithOverviewMode(true); mWebView.getSettings().setUseWideViewPort(true); if (Build.VERSION.SDK_INT < 17) { Log.i("GPSNETWORK", "<17"); } else { Log.i("GPSNETWORK", Build.VERSION.SDK_INT+">=17"); mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false); } mWebView.loadData(vid, "text/html", "UTF-8"); } }
-
user2955394 almost 10 yearsthank you but it's not work for me(not error but still not auto play)
-
user2955394 almost 10 yearswhen i run my app the video show play button and when i click play button it's work well. But i want to auto play it's don't want to click play button.
-
user2955394 almost 10 yearsi try to do with your suggestion. But it's still no auto play. Plese see in my question i edit it's with your suggestion. i check my SDK version is 18
-
Jaydev almost 8 years@user2955394 - Did you find a solution for this ?