Reputation: 127
I'm loading a youtube video in a webview but the problem is that video is not playing automatically just like it does when we go to actual youtube website.In the webview video does shows up and plays but only after i push the play button which appears on the top of video.So i wanted to know if there is any way by which i can make videos play within the webview once the URL has been loaded.Following is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_view);
WebView wv = (WebView) findViewById(R.id.webView);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginState(PluginState.ON);
final String mimeType = "text/html";
final String encoding = "UTF-8";
String html = getHTML();
wv.setWebChromeClient(new WebChromeClient() {
});
wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
}
public String getHTML() {
String html = "<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 95%; padding:0px; margin:0px\" id=\"ytplayer\" type=\"text/html\" src=\"http://www.youtube.com/embed/"
+ "J2fB5XWj6IE"
+ "?fs=0\" frameborder=\"0\">\n"
+ "</iframe>\n";
return html;
}
Thank You!!!
Upvotes: 1
Views: 4362
Reputation: 578
Follow this code, your video will run well
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setAllowFileAccess(true);
//webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
webView.setBackgroundColor(Color.BLACK);
webView.getSettings().setUserAgentString("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36");
//webView.addJavascriptInterface(this, "nativeInterface");
if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
String youtubeID = ""; //get Youtube ID and input here
String url = "http://www.youtube.com/embed/" +
youtubeID +
"?autoplay=1";
webView.loadUrl(url);
Upvotes: 0
Reputation: 16235
Doing Youtube is kind of tricky. First you will need try to access the mobile site. Second you need to play it in a popup window. The playback needs to be done in the WebChromClient#onShowCustomView method. I kind of forgot the details but that is the general idea. The "view" parameter of the onShowCustomView method, if I remember correctly is a layout with a video view in it, you can start it to play.
Upvotes: 0
Reputation: 1482
add "autoplay=1" to your url
String html = "<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 95%; padding:0px; margin:0px\" id=\"ytplayer\" type=\"text/html\" src=\"http://www.youtube.com/embed/"
+ "J2fB5XWj6IE?autoplay=1"
+ "&fs=0\" frameborder=\"0\">\n"
+ "</iframe>\n";
Upvotes: 4