Wednesday, September 28, 2011

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

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.