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

20 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
  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