Android YouTube app Play Video Intent

225,008

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.

Share:
225,008

Related videos on Youtube

Isaac Waller
Author by

Isaac Waller

currently ----

Updated on June 08, 2021

Comments

  • Isaac Waller
    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
      Jaydip Kalkani over 6 years
      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
  • Isaac Waller
    Isaac Waller over 15 years
    Do you know the unsupported way?
  • Mukesh Nandeda Dhakad
    Mukesh Nandeda Dhakad over 14 years
    This was the winner for me, if you have the youtube app installed it asks if you want to use that instead.
  • Praveen
    Praveen about 14 years
    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.
  • Mr. Bhosale
    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
    Symmetric over 13 years
    You'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
    Vinothkumar Arputharaj about 13 years
    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.Playe‌​rActivity}; have you declared this activity in your AndroidManifest.xml? Kindly resolve this.
  • Sana
    Sana about 13 years
    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.
  • Mathias Conradt
    Mathias Conradt over 12 years
    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.
  • The iCoder
    The iCoder over 12 years
    This is also not worked for me. Is there any requiremnt of flash player in emulator.
  • Mitch
    Mitch over 12 years
    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.
  • IgorGanapolsky
    IgorGanapolsky about 12 years
    What is Utility? I don't see it as an available import.
  • Bob Barbara
    Bob Barbara about 12 years
    +1 for the EDIT. Using an explicit class name is definitely unreliable.
  • r1k0
    r1k0 almost 12 years
    This directly starts the YouTube application, bypassing the chooser menu in which you can choose between chrome and YouTube on JellyBean.
  • Soubhab Pathak
    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
    Scorpion over 11 years
    Hello, Is it possible to play youtube videos in App instead of playing it in Webview?
  • Matthewek
    Matthewek about 11 years
    @Scorpion There is youtube API, with such possibility.
  • sajith
    sajith almost 11 years
    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
  • bass.t
    bass.t about 10 years
    mhhh. same same but different. except you have a one liner ;) but this also works.
  • Luke Sapan
    Luke Sapan about 9 years
    Props 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
    Rusheel Jain almost 9 years
    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"
  • voghDev
    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.
    Nacho L. over 8 years
    IMHO this should be the accepted answer. Thanks a lot!
  • KostasC
    KostasC over 8 years
    It works great! But your are missing one right parenthesis on the first line.
  • Redoman
    Redoman about 8 years
    If you have more than one youtube player app (example Firetube) it will still make you choose. Just saying and making it clear.
  • Vinod Supnekar
    Vinod Supnekar over 7 years
    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.
  • thanhbinh84
    thanhbinh84 over 7 years
    stackoverflow.com/a/12439378/578309 is longer but safer, this solution shorter but might not work on some devices.
  • Ampersanda
    Ampersanda about 7 years
    This is the answer but maybe you should remove static in the function declaration
  • Dan
    Dan almost 7 years
    Awesome! So did you just create a broadcast receiver for this?
  • santosh devnath
    santosh devnath about 5 years
    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.