Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed Youtube video inside an Android app

I'm using a WebView for displaying embedded Youtube video and that works on Galaxcy S2 (OS 2.3.5) and doesn't on Nexus S (OS 2.3.4), all I get is white screen without any video display.

Here is the code snippet I'm using and the declarations in Manifest file:

private WebView wv;  private void setWebView() { wv = (WebView) findViewById(R.id.webView);  wv.setWebChromeClient(new WebChromeClient());  wv.getSettings().setPluginState(WebSettings.PluginState.ON);  wv.setWebViewClient(new WebViewClient());   wv.getSettings();  wv.setBackgroundColor(0x00000000);  wv.setKeepScreenOn(true);  wv.setHorizontalScrollBarEnabled(false); wv.setVerticalScrollBarEnabled(false);  wv.getSettings().setBuiltInZoomControls(true);  final String mimeType = "text/html"; final String encoding = "UTF-8"; String html = getHTML();  wv.loadDataWithBaseURL("", html, mimeType, encoding, "");  }   public String getHTML() {  String html = "<html>"      + "<head>"  + "</head>"  + "<body style=\"border: 0; padding: 0\">"  + "<iframe "  + "type=\"text/html\" "  + "class=\"youtube-player\" "  + "width= 100%\""  + "\" "  + "height= 95%\""  + "\" "  + "src=\"http://www.youtube.com/v/"  + selected_video      + "?controls=0&showinfo=0&showsearch=0&modestbranding=0" +  "&autoplay=1&fs=1&vq=hd720\" " + "frameborder=\"0\"></iframe>"      + "</body>"     + "</html>";   return html; } 

Note: the parameter "selected_video" is the hash of the video (VideoID).

The declarations in Manifest file:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=http://schemas.android.com/apk/res/android . . <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"  />  <application    .    .     android:hardwareAccelerated="true"  >      .     . 

Please let me know in case you recognizing anything I should change in my code, or help with a complete code which can support all Android devices and OS for displaying embedded (In-App) Youtube video with high quality.

UPDATE: Pay attention, the solution I'm looking for should display high resolution video. I got it work on the different devices and OS using VideoView class but the video quality isn't good enough. So any solution including VideoView or WebView or any other way will be accepted only if it makes high quality YouTube video to be displayed. Thanks to all the responders!

like image 780
Idan Avatar asked Jun 12 '13 18:06

Idan


People also ask

Can I embed YouTube videos on my app?

No problem at all. If you are just displaying someone's video in your app, just be sure not to have ads in that activity.

How do you embed a YouTube video on mobile?

YouTube has an option on the embed button called 'use old embed link' that gives you an embed code. If you want it to serve a video according to the device, you are out of luck. The problem is that the old embed code is flash based, so it will play inline on newer android devices.


2 Answers

there is an official YouTube Android Player API wich you can use. This is a bit more complicated but it is working better than other solutions using webclients.

First you must register your app in Googles API Console. This is completely free until your app gets over 25k request a month (or something like that). There are complete anf great tutorials under the link. I hope you can understand them. If not, ask! :)

like image 123
raptor Avatar answered Oct 18 '22 10:10

raptor


How it looks:

enter image description here

Best solution to my case. I need video fit web view size. Use embed youtube link with your video id. Example:

WebView youtubeWebView; //todo find or bind web view String myVideoYoutubeId = "-bvXmLR3Ozc";  outubeWebView.setWebViewClient(new WebViewClient() {             @Override             public boolean shouldOverrideUrlLoading(WebView view, String url) {                 return false;             }         });  WebSettings webSettings = youtubeWebView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setLoadWithOverviewMode(true); webSettings.setUseWideViewPort(true);  youtubeWebView.loadUrl("https://www.youtube.com/embed/" + myVideoYoutubeId); 

Web view xml code

<WebView         android:id="@+id/youtube_web_view"         android:layout_width="match_parent"         android:layout_height="200dp"/> 
like image 29
Рома Богдан Avatar answered Oct 18 '22 11:10

Рома Богдан