Thursday, April 22, 2010

Android YouTube Intent

For a long time I've wondered how to show a YouTube video to the user in an Android application. There's this awesome post published by KeyesLabs on how to create your own Activity that plays YouTube videos. It's great, and you should definitely use it. But I think you can improve on that. It would be very useful for the user to view that video in the default YouTube player installed on the device because this way they can save it (like it, rate it, save it to their profile) plus enjoy other improvements and features the official YouTube app provides (plus probably better error checking for unavailable videos and so on).

While I was playing around with the emulator, I noticed that if you try to view a YouTube video in it th browser gives an error similar to Cannot open the page at vnd.youtube:VIDEO_ID?some=other¶meters=here. This way, I learned that a VIEW intent with a data URI like vnd.youtube:VIDEO_ID will launch the official YouTube app (this was confirmed by some nice folks on IRC, as I don't have an Android device). Basically:

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:VIDEO_ID")); startActivity(i);

Will launch the YouTube app and watch the video with ID VIDEO_ID. Couple this with the Activity on KeyesLabs' blog and the Can I use this Intent? article and you've got a winner. My final solution is:

private void startVideo(String videoID) { // default youtube app Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + videoID)); List<ResolveInfo> list = getPackageManager().queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY); if (list.size() == 0) { // default youtube app not present or doesn't conform to the standard we know // use our own activity i = new Intent(getApplicationContext(), YouTube.class); i.putExtra("VIDEO_ID", videoID); } startActivity(i); }

47 comments:

  1. Very helpful. Thank you!!

    ReplyDelete
  2. Is it works on emulator??

    ReplyDelete
  3. well nice post.... by using this api. any one can easily watch You-tube in their android phones.Now you can watch anything you wish too or can upload any video you wish too share on You tube.

    ReplyDelete
  4. Hi,
    Where can I find the YouTube.class???
    Thanks,
    Adrian

    ReplyDelete
    Replies
    1. use IntroVideoActivity.class

      Delete
    2. I used introVideoActivity.java but it gives me nullException on

      String lVideoSchemeStr = lVideoIdUri.getScheme();

      Delete
  5. how do you sniff the VIDEO_ID from a url?

    ReplyDelete
  6. Here is how to get video id from a link

    Uri uri = Uri.parse(url);
    String vid = uri.getQueryParameter("v");

    ReplyDelete
  7. How can I run my own media player application when I want to watch a video from Youtube?

    ReplyDelete
  8. brillaint dude, i have been having same problem but i have tried your method and can get video to appear but it doesnt play when clicked. can you add source code please to help me. thanks

    ReplyDelete
  9. this starts playing the video as well as starts browser

    ReplyDelete
  10. it should not start the browser, do anybody know on this issue

    ReplyDelete
  11. do we have to edit the manifestation file for the activity?

    ReplyDelete
  12. Hi,
    Where can I find the YouTube.class???
    Thanks in advance

    ReplyDelete
  13. in android : is there any way on completion of the youtube video it return back to previous activity? or any parameters that knows completion of video.

    ReplyDelete
  14. Great blog friend....
    It was very helpful
    Keep blogging...

    ReplyDelete
  15. How do you play the youtube video in your own activity, the alternative you mentioned? Using MediaPlayer or html5+WebView?

    ReplyDelete
  16. I am using the exact same code to open a youtube app from within the android app. But I want to open the video in fullscreen mode.

    Anyone knows how to open a video in youtube in fullscreen mode by default? And also to get back to the app when the video completes - will be a nice to have feature.

    Thanks!

    ReplyDelete
  17. hello could you please tell me that where i can find the Youtube.

    ReplyDelete
  18. Your blog information is really usable.I like your blog features.This is one of the efficient post.

    ReplyDelete
  19. Can this play the actual HD video or just the LQ / HQ from the RTSP link?

    ReplyDelete
  20. You can also this android application for so many different reasons.Android based applications are in great demand today in market.how to develop apps

    ReplyDelete
  21. how can i download the video can you give any exanmple please?

    ReplyDelete
  22. Where are you supposed to input the code ?

    ReplyDelete
  23. Thanks it's working properly,
    and i tried on device too.
    But it delays lot, please tell me how to reduce time to play video.

    ReplyDelete
  24. Replies
    1. Hi, @raj kumar
      Please, tell me where do you stuck...?

      Delete
  25. I am able to launch the video in youtube app but i want to return to my main activity once the video gets over. Can anyone guide me how can i do that?

    Thanks,
    CS

    ReplyDelete
  26. A utilise erotic gay sizegenetics is that it reduces tension. This can be a great therapy for anybody who has chronic anxiety. Yet it will not be merely a matter of relief; it has been proven that your respiration will get better and you will then have more white blood cell. The more white blood cells you have, better your body will capable to fend off you will find a cold and other illnesses. For anyone who will have a busy life, the reduction of stress levels is really a great reason to seek man to man sizegenetics, especially therapeutic gay sizegenetics.
    http://buysizegeneticsonline.tumblr.com/

    ReplyDelete
  27. thank you..........thank you very much u save my lots of time i have already west my lots of time with OpenYouTubePlayerActivity. But due to your tutorial i am really very satisfied. Thank you again and keep it up..........

    ReplyDelete
  28. Please give me a code for download you tube video

    ReplyDelete
  29. it took me a long time searching to find this.
    Thanks. stackoverflow hasn't this info.

    ReplyDelete
  30. hello, if i only want to open Youtube app main page, what's the code?

    Thank in advance

    ReplyDelete
    Replies
    1. Hello,
      get you tube app pkg name and refer this
      http://stackoverflow.com/questions/3422758/start-application-knowing-package-name/9976165#9976165

      Delete
  31. it is not opening youtube application instead it is opening youtube in browser

    ReplyDelete
  32. Thanks for good post.

    www.handysofts.com

    ReplyDelete
  33. Use this diet hack to drop 2 lb of fat in just 8 hours

    At least 160k men and women are trying a easy and SECRET "water hack" to burn 1-2 lbs every night while they sleep.

    It is proven and works all the time.

    This is how to do it yourself:

    1) Go grab a glass and fill it up half glass

    2) And now do this proven hack

    you'll become 1-2 lbs skinnier as soon as tomorrow!

    ReplyDelete
  34. According to Stanford Medical, It's in fact the ONLY reason this country's women get to live 10 years more and weigh on average 19 KG lighter than we do.

    (And actually, it has NOTHING to do with genetics or some secret diet and absolutely EVERYTHING to do with "HOW" they eat.)

    BTW, I said "HOW", and not "WHAT"...

    Tap on this link to find out if this little quiz can help you find out your true weight loss possibility

    ReplyDelete
  35. Great article! The information you have shared is very engaging and impressive. It’s hard to come by experienced people talking about this subject! Thank you. Visit our reliable Dissertation Finishing Service for assistance with completing your research project.

    ReplyDelete
  36. Compre documentos en línea, documentos originales y registrados.
    Acerca de Permisodeespana, algunos dicen que somos los solucionadores de problemas, mientras que otros se refieren a nosotros como vendedores de soluciones. Contamos con cientos de clientes satisfechos a nivel mundial. Hacemos documentos falsos autorizados y aprobados como Permiso de Residencia Español, DNI, Pasaporte Español y Licencia de Conducir Española. Somos los fabricantes y proveedores de primer nivel de estos documentos, reconocidos a nivel mundial.

    Comprar permiso de residencia,
    permiso de residenciareal y falso en línea,
    Compre licencia de conducir en línea,
    Compre una licencia de conducir española falsa en línea,
    Comprar tarjeta de identificación,
    Licencia de conducir real y falsa,
    Compre pasaporte real en línea,

    Visit Here fpr more information. :- https://permisodeespana.com/licencia-de-conducir-espanola/
    Address: 56 Guild Street, London, EC4A 3WU (UK)
    Email: contact@permisodeespana.com
    WhatsApp: +443455280186

    ReplyDelete

  37. It is very helpful and informative blog post. I would like to thankful to you providing such a information I have also have website providing very good information
    evs full form
    raw agent full form
    full form of tbh in instagram
    dbs bank full form
    https full form
    tft full form
    pco full form
    kra full form in hr
    tbh full form in instagram story
    epc full form

    ReplyDelete