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);
}
Very helpful. Thank you!!
ReplyDeleteIs it works on emulator??
ReplyDeletewell 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.
ReplyDeleteHi,
ReplyDeleteWhere can I find the YouTube.class???
Thanks,
Adrian
use IntroVideoActivity.class
DeleteI used introVideoActivity.java but it gives me nullException on
DeleteString lVideoSchemeStr = lVideoIdUri.getScheme();
how do you sniff the VIDEO_ID from a url?
ReplyDeleteHere is how to get video id from a link
ReplyDeleteUri uri = Uri.parse(url);
String vid = uri.getQueryParameter("v");
How can I run my own media player application when I want to watch a video from Youtube?
ReplyDeletebrillaint 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
ReplyDeletethis starts playing the video as well as starts browser
ReplyDeleteit should not start the browser, do anybody know on this issue
ReplyDeletedo we have to edit the manifestation file for the activity?
ReplyDeletevery helpful post
ReplyDeleteHi,
ReplyDeleteWhere can I find the YouTube.class???
Thanks in advance
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.
ReplyDeleteGreat blog friend....
ReplyDeleteIt was very helpful
Keep blogging...
How do you play the youtube video in your own activity, the alternative you mentioned? Using MediaPlayer or html5+WebView?
ReplyDeleteI 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.
ReplyDeleteAnyone 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!
hello could you please tell me that where i can find the Youtube.
ReplyDeleteYour blog information is really usable.I like your blog features.This is one of the efficient post.
ReplyDeleteCan this play the actual HD video or just the LQ / HQ from the RTSP link?
ReplyDeleteplays other than RTSP link.
DeleteYou can also this android application for so many different reasons.Android based applications are in great demand today in market.how to develop apps
ReplyDeletehow can i download the video can you give any exanmple please?
ReplyDeleteWhere are you supposed to input the code ?
ReplyDeleteThanks it's working properly,
ReplyDeleteand i tried on device too.
But it delays lot, please tell me how to reduce time to play video.
any one plz guide me plz
ReplyDeleteHi, @raj kumar
DeletePlease, tell me where do you stuck...?
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?
ReplyDeleteThanks,
CS
That is some inspirational stuff. Never knew that opinions could be this varied. Thanks for all the enthusiasm to offer such helpful information here.
ReplyDeleteApp Development Company
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.
ReplyDeletehttp://buysizegeneticsonline.tumblr.com/
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..........
ReplyDeletePlease give me a code for download you tube video
ReplyDeleteit took me a long time searching to find this.
ReplyDeleteThanks. stackoverflow hasn't this info.
thank you!
ReplyDelete