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); }

41 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. That is some inspirational stuff. Never knew that opinions could be this varied. Thanks for all the enthusiasm to offer such helpful information here.
    App Development Company

    ReplyDelete
  27. 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
  28. 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
  29. Please give me a code for download you tube video

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

    ReplyDelete
  31. 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
  32. it is not opening youtube application instead it is opening youtube in browser

    ReplyDelete
  33. Thanks for good post.

    www.handysofts.com

    ReplyDelete