Android YouTube app Play Video Intent
Solution 1
And how about this:
public static void watchYoutubeVideo(Context context, String id){
Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id));
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=" + id));
try {
context.startActivity(appIntent);
} catch (ActivityNotFoundException ex) {
context.startActivity(webIntent);
}
}
Solution 2
This will work on a device but not the emulator per Lemmy's answer.
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM")));
Solution 3
Here's how I solved this issue:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + video_id));
startActivity(intent);
Now that I've done some more research, it looks like I only needed 'vnd.youtube:VIDEO_ID' instead of two slashes after the colon (':' vs. '://'):
http://it-ride.blogspot.com/2010/04/android-youtube-intent.html
I tried most of the suggestions here and they didn't really work very well with all of the supposed "direct" methods raising exceptions. I would assume that, with my method, if the YouTube app is NOT installed, the OS has a default fallback position of something other than crashing the app. The app is theoretically only going on devices with the YouTube app on them anyway, so this should be a non-issue.
Solution 4
Use my code .. I am able to play youtube video using this code ... you just need to provide youtube video id in the "videoId" variable ....
String videoId = "Fee5vbFLYM4";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+videoId));
intent.putExtra("VIDEO_ID", videoId);
startActivity(intent);
Solution 5
Intent videoClient = new Intent(Intent.ACTION_VIEW);
videoClient.setData(Uri.parse("http://m.youtube.com/watch?v="+videoId));
startActivityForResult(videoClient, 1234);
Where videoId
is the video id of the youtube video that has to be played. This code works fine on Motorola Milestone.
But basically what we can do is to check for what activity is loaded when you start the Youtube app and accordingly substitute for the packageName and the className.
Related videos on Youtube
Comments
-
Isaac Waller about 3 years
I have created a app where you can download YouTube videos for android. Now, I want it so that if you play a video in the YouTube native app you can download it too. To do this, I need to know the Intent that the YouTube native app puts out in order to play the YouTube app.
I could do this easially if I had the YouTube program on my emulator, so my 1st question is:
1. Can I download the YouTube app for my emulator, or...
2. What is the intent used when the user selects a video for playback.-
Jaydip Kalkani over 6 yearshey, i am also trying to make an youtube video downloader app. So, i want to know that i have to get any api key for that or simply i can use webview in my app?? @ASP
-
-
Isaac Waller over 15 yearsDo you know the unsupported way?
-
Mukesh Nandeda Dhakad over 14 yearsThis was the winner for me, if you have the youtube app installed it asks if you want to use that instead.
-
Praveen about 14 yearsuse this code i get the intent chooser for youtube and browser. how to hide it? i just need to directly link up to youtube player.
-
Mr. Bhosale almost 14 years@Praveen Chandrasekaran the youtube app may not be available - it's best to use the Intent, and let the system work out which installed apps support the request.
-
Symmetric over 13 yearsYou'll need to call setClassName on the Intent before calling startActivity, like this: intent.videoClient.setClassName("com.google.android.youtube", "com.google.android.youtube.PlayerActivity"); See Sana's answer for a working code example.
-
Vinothkumar Arputharaj about 13 yearsI'm getting the following error.
ERROR/AndroidRuntime(1519): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.google.android.youtube/com.google.android.youtube.PlayerActivity}; have you declared this activity in your AndroidManifest.xml?
Kindly resolve this. -
Sana about 13 yearsWhat is your device? This activity name is only specific for a Motorola Milestone, but if you want to see the activity name for the youtube activity plug in your device in USB debugging mode and see the LogCat when you click on the Youtube App. It should give you the package name alogn with activity name.
-
Mathias Conradt over 12 yearsThis doesn't work for me, on a real device with youtube app installed. It still opens the web browser. The approach by Bibbity below works though. Or maybe Symmetric's comment, haven't tried that.
-
The iCoder over 12 yearsThis is also not worked for me. Is there any requiremnt of flash player in emulator.
-
Mitch over 12 yearsThis doesn't work for me at all on a real device. Simply says it can't play the video. I do have YouTube installed.
-
IgorGanapolsky about 12 yearsWhat is Utility? I don't see it as an available import.
-
Bob Barbara about 12 years+1 for the EDIT. Using an explicit class name is definitely unreliable.
-
r1k0 almost 12 yearsThis directly starts the YouTube application, bypassing the chooser menu in which you can choose between chrome and YouTube on JellyBean.
-
Soubhab Pathak almost 12 years@r1k0 Yes .... This code will directly start the default Youtube app to play the youtube video in all types of android oses(not only in JellyBean).
-
Scorpion over 11 yearsHello, Is it possible to play youtube videos in App instead of playing it in Webview?
-
Matthewek about 11 years@Scorpion There is youtube API, with such possibility.
-
sajith almost 11 yearswhile using this code i am able to play video,but after i chose my browser its loading youtube webpage and i need to click,is there any way to avoid this step
-
bass.t about 10 yearsmhhh. same same but different. except you have a one liner ;) but this also works.
-
Luke Sapan about 9 yearsProps for supporting the YouTube app if it's available, and falling back to the web browser if necessary. For those coming to this question, this is the answer to use.
-
Rusheel Jain almost 9 yearsHow to launch youtube app for search via keyword eg. i want the youtube app to be launched and then in that search for "steve jobs"
-
voghDev over 8 years@RusheelJain that would be a new issue. What I'd do is search for "android youtube intent search text" or something like that.
-
Nacho L. over 8 yearsIMHO this should be the accepted answer. Thanks a lot!
-
KostasC over 8 yearsIt works great! But your are missing one right parenthesis on the first line.
-
Redoman about 8 yearsIf you have more than one youtube player app (example Firetube) it will still make you choose. Just saying and making it clear.
-
Vinod Supnekar over 7 years
Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id));
In this line removingvnd.youtube
worked for me , as it was appending extra URL of youtube.com toid
url. -
thanhbinh84 over 7 yearsstackoverflow.com/a/12439378/578309 is longer but safer, this solution shorter but might not work on some devices.
-
Ampersanda about 7 yearsThis is the answer but maybe you should remove static in the function declaration
-
Dan almost 7 yearsAwesome! So did you just create a broadcast receiver for this?
-
santosh devnath about 5 yearsRemember that alwasy - Google never allows playing youtube video inside Webview. Because they provide Youtube API to play Youtube video on Android App. If you will play Youtube video inside the webview, then Google will remove your application from Playstore.