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.

- 187,060
- 113
- 301
- 369

- 32,709
- 29
- 96
- 107
-
hey, 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 – Jaydip Kalkani Mar 15 '18 at 08:04
19 Answers
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);
}
}

- 6,554
- 2
- 28
- 24
-
6Props 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. – Leah Sapan May 30 '15 at 22:04
-
How 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" – Rusheel Jain Sep 13 '15 at 09:20
-
@RusheelJain that would be a new issue. What I'd do is search for "android youtube intent search text" or something like that. – voghDev Oct 01 '15 at 09:23
-
`Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id)); ` In this line removing `vnd.youtube` worked for me , as it was appending extra URL of youtube.com to `id` url. – Vinod Supnekar Oct 13 '16 at 13:17
-
1This is the answer but maybe you should remove static in the function declaration – Ampersanda May 20 '17 at 12:51
-
How do I know that parsing the URI with "vnd.youtube" + id opens the yt-app directly? Is there a documentation where such "shortcuts" are defined? I mean a "mailto:" is somewhat common but "vnd.youtube"??? – citizen_code Jul 03 '23 at 19:01
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")));
-
This was the winner for me, if you have the youtube app installed it asks if you want to use that instead. – Tristan Warner-Smith Mar 23 '10 at 16:37
-
use 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. – Praveen May 21 '10 at 14:13
-
@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. – cofiem Sep 23 '10 at 12:27
-
3You'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. – Symmetric Jan 28 '11 at 15:50
-
This 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. – Mathias Conradt Nov 11 '11 at 08:10
-
This doesn't work for me at all on a real device. Simply says it can't play the video. I do have YouTube installed. – Mitch Mar 18 '12 at 23:05
-
while 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 – sajith Jul 25 '13 at 09:02
-
mhhh. same same but different. except you have a one liner ;) but this also works. – bass.t Jun 10 '14 at 14:49
-
http://stackoverflow.com/a/12439378/578309 is longer but safer, this solution shorter but might not work on some devices. – thanhbinh84 Feb 10 '17 at 11:33
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.

- 35,772
- 9
- 166
- 188

- 391
- 3
- 4
-
Hello, Is it possible to play youtube videos in App instead of playing it in Webview? – Scorpion Oct 08 '12 at 06:07
-
-
1It works great! But your are missing one right parenthesis on the first line. – KostasC Jan 17 '16 at 13:58
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);

- 619
- 7
- 14
-
3This directly starts the YouTube application, bypassing the chooser menu in which you can choose between chrome and YouTube on JellyBean. – r1k0 Aug 29 '12 at 08:16
-
2@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). – Soubhab Pathak Aug 29 '12 at 14:00
-
1If you have more than one youtube player app (example Firetube) it will still make you choose. Just saying and making it clear. – Redoman Apr 12 '16 at 03:56
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.

- 2,190
- 6
- 21
- 35

- 9,895
- 15
- 59
- 87
-
I'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. – Vinothkumar Arputharaj Jun 14 '11 at 14:06
-
What 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. – Sana Jun 15 '11 at 02:00
The Youtube (and Market application) are only supposed to be used with special ROMs, which Google released for the G1 and the G2. So you can't run them in an OpenSource-ROM, like the one used in the Emulator, unfortunately. Well, maybe you can, but not in an officially supported way.

- 3,177
- 3
- 17
- 16
Found it:
03-18 12:40:02.842: INFO/ActivityManager(68): Starting activity: Intent { action=android.intent.action.VIEW data=(URL TO A FLV FILE OF THE VIDEO) type=video/* comp={com.google.android.youtube/com.google.android.youtube.YouTubePlayer} (has extras) }

- 32,709
- 29
- 96
- 107
EDIT: The below implementation proved to have problems on at least some HTC devices (they crashed). For that reason I don't use setclassname and stick with the action chooser menu. I strongly discourage using my old implementation.
Following is the old implementation:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(youtubelink));
if(Utility.isAppInstalled("com.google.android.youtube", getActivity())) {
intent.setClassName("com.google.android.youtube", "com.google.android.youtube.WatchActivity");
}
startActivity(intent);
Where Utility is my own personal utility class with following methode:
public static boolean isAppInstalled(String uri, Context context) {
PackageManager pm = context.getPackageManager();
boolean installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}
First I check if youtube is installed, if it is installed, I tell android which package I prefer to open my intent.

- 2,190
- 6
- 21
- 35

- 27,966
- 19
- 103
- 155
-
-
+1 for the EDIT. Using an explicit class name is definitely unreliable. – Giulio Piancastelli Jun 21 '12 at 08:46
The safest way to run videos on a different app is by first trying to resolve the package, in other words, check that the app is installed on the device. So if you want to run a video on youtube you'd do something like this:
public void playVideo(String key){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + key));
// Check if the youtube app exists on the device
if (intent.resolveActivity(getPackageManager()) == null) {
// If the youtube app doesn't exist, then use the browser
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=" + key));
}
startActivity(intent);
}

- 438
- 1
- 6
- 10
Replying to old question, just to inform you guys that package have changed, heres the update
Intent videoClient = new Intent(Intent.ACTION_VIEW);
videoClient.setData("VALID YOUTUBE LINK WITH HTTP");
videoClient.setClassName("com.google.android.youtube", "com.google.android.youtube.WatchActivity");
startActivity(videoClient);
This works very well, but when you call normal Intent with ACTION_VIEW with valid youtube URL user gets the Activity selector anyways.

- 12,674
- 8
- 66
- 99

- 27,760
- 6
- 37
- 35
/**
* Intent to open a YouTube Video
*
* @param pm
* The {@link PackageManager}.
* @param url
* The URL or YouTube video ID.
* @return the intent to open the YouTube app or Web Browser to play the video
*/
public static Intent newYouTubeIntent(PackageManager pm, String url) {
Intent intent;
if (url.length() == 11) {
// youtube video id
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + url));
} else {
// url to video
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
}
try {
if (pm.getPackageInfo("com.google.android.youtube", 0) != null) {
intent.setPackage("com.google.android.youtube");
}
} catch (NameNotFoundException e) {
}
return intent;
}

- 37,824
- 19
- 133
- 148
This will work if youtube app installed. If not, a chooser will show up to select other application:
Uri uri = Uri.parse( "https://www.youtube.com/watch?v=bESGLojNYSo" );
uri = Uri.parse( "vnd.youtube:" + uri.getQueryParameter( "v" ) );
startActivity( new Intent( Intent.ACTION_VIEW, uri ) );

- 2,376
- 2
- 24
- 38
You can also just grab the WebViewClient
wvClient = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("youtube:")) {
String youtubeUrl = "http://www.youtube.com/watch?v="
+ url.Replace("youtube:", "");
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(youtubeUrl)));
}
return false;
}
Worked fine in my app.

- 398,947
- 96
- 818
- 769

- 81
- 1
- 4
-
Remember 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. – santosh devnath Jun 01 '19 at 10:25
This function work fine for me...just pass url string in function:
void watch_video(String url)
{
Intent yt_play = new Intent(Intent.ACTION_VIEW, Uri.parse(url))
Intent chooser = Intent.createChooser(yt_play , "Open With");
if (yt_play.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
}

- 1,784
- 19
- 23
Try this:
public class abc extends Activity implements OnPreparedListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM")));
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
}
}
}
Try this,
WebView webview = new WebView(this);
String htmlString =
"<html> <body> <embed src=\"youtube link\"; type=application/x-shockwave-flash width="+widthOfDevice+" height="+heightOfDevice+"> </embed> </body> </html>";
webview.loadData(htmlString ,"text/html", "UTF-8");

- 2,190
- 6
- 21
- 35

- 57
- 1
- 6
-
This is also not worked for me. Is there any requiremnt of flash player in emulator. – The iCoder Nov 16 '11 at 06:15
You can use the Youtube Android player API to play the video if Youtube app is installed, otherwise just prompt the user to choose from the available web browsers.
if(YouTubeIntents.canResolvePlayVideoIntent(mContext)){
mContext.startActivity(YouTubeIntents.createPlayVideoIntent(mContext, mVideoId));
}else {
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=" + mVideoId));
mContext.startActivity(webIntent);
}
Use with Kotlin Extension
much easier.
fun Context.watchYoutube(id: String) {
val appIntent = Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:$id"))
val webIntent = Intent(
Intent.ACTION_VIEW,
Uri.parse("https://youtu.be/$id")
)
try {
this.startActivity(appIntent)
} catch (ex: ActivityNotFoundException) {
this.startActivity(webIntent)
}
}
And then you can implement it at the MainActivity.kt
like this
fun onClickSomething(){
val linkYoutubeId = "https://youtu.be/3s21Ynn4Huw".substringAfterLast("/")
watchYoutube(linkYoutubeId)
}

- 2,738
- 4
- 20
- 46