Skip to content

Instantly share code, notes, and snippets.

@Zulqurnain
Last active March 19, 2018 00:25
Show Gist options
  • Save Zulqurnain/b666f1ff77e68dbecb258f53b47f8496 to your computer and use it in GitHub Desktop.
Save Zulqurnain/b666f1ff77e68dbecb258f53b47f8496 to your computer and use it in GitHub Desktop.
Play Youtube Video Using WebView
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
/**
* Created by Zulqurnain on 19/03/2018.
*/
public class YoutubeUtils {
public static String getHTML(String videoId) {
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/" + videoId
+ "?fs=0\" frameborder=\"0\" " + "allowfullscreen autobuffer "
+ "controls onclick=\"this.play()\""
+ ">\n" + "</iframe>\n";
/**
* <iframe id="ytplayer" type="text/html" width="640" height="360"
* src="https://www.youtube.com/embed/WM5HccvYYQg" frameborder="0"
* allowfullscreen>
**/
return html;
}
public static void PlayVideo(WebView v,String youtubeVideoID){
v.getSettings().setJavaScriptEnabled(true);
if (Build.VERSION.SDK_INT < 8) {
v.getSettings().setPluginsEnabled(true);
} else {
v.getSettings().setPluginState(WebSettings.PluginState.ON);
}
v.setWebChromeClient(new WebChromeClient() {
});
final String mimeType = "text/html";
final String encoding = "UTF-8";
String html = getHTML(youtubeVideoID);
// String html = getHTML("jxPGCefNu88"); // example
v.loadDataWithBaseURL("", html, mimeType, encoding, "");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment