Play YouTube videos with YouTube app from webView

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Play YouTube videos with YouTube app from webView

Postby NameTaken33 » Fri Jan 14, 2011 10:15 pm

So I've leeched here quite a bit and haven't contributed at all so I figure I would share this. This is was bothering me for about 2 days straight until I figured it out.

First you need to set up webView on your Android App with Intent. Intent will allow you to open the actual YouTube app from your webview app.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. YOUR PACKACGE NAME SHOULD GO HERE
  2.  
  3. import android.app.Activity;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.webkit.WebView;
  7. import android.webkit.WebViewClient;
  8. import android.content.Intent;
  9. import android.net.Uri;
  10.  
  11. public class main extends Activity {
  12.     /** Called when the activity is first created. */
  13.      @ Override
  14.      
  15.      
  16.      public void onCreate (Bundle savedInstanceState) {
  17.          super. onCreate (savedInstanceState);
  18.        
  19.  
  20.      
  21.          setContentView (R.layout.main);
  22.  
  23.          WebView web = (WebView) findViewById (R.id.webView);
  24.          web. getSettings().setJavaScriptEnabled (true);
  25.          web. getSettings().setJavaScriptCanOpenWindowsAutomatically (false);
  26.          web. getSettings().setPluginsEnabled (true);
  27.          web. getSettings().setSupportMultipleWindows (false);
  28.          web. getSettings().setSupportZoom (false);
  29.          web. setVerticalScrollBarEnabled (false);
  30.          web. setHorizontalScrollBarEnabled (false);
  31.  
  32.        
  33.          web. loadUrl ("THE URL TO YOUR WEBVIEW SITE SHOULD GO HERE");
  34.  
  35.          web. setWebViewClient (new WebViewClient () {
  36.              @ Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
  37.              if (url.startsWith("vnd.youtube")){
  38.                  
  39.              startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
  40.              
  41.              return true;
  42.              }
  43.              else
  44.              {
  45.              return false;
  46.              }
  47.              }
  48.          });
  49.      }
  50.      
  51.  }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Then when linking videos from your site to YouTube to open the YouTube app all you need is:

<a href="vnd.youtube://VIDEOID">WATCH ME</a>

Simply replace VIDEOID, with the actual video ID of the video you want to watch and when you click it, it will launch the YouTube app with the video.

I'm going to go cry now that something so simple took me forever to figure out. >_< Hopefully others can find this helpful.
User avatar
NameTaken33
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Jan 07, 2011 9:20 pm

Top

Re: Play YouTube videos with YouTube app from webView

Postby eazyigz » Thu Mar 01, 2012 11:12 pm

I wish this worked for any YouTube videos being loaded through the WebView Client. Currently, when I press the play button on a YouTube video in the WebView, nothing happens.
eazyigz
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Mar 01, 2012 11:10 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests