Android YouTube app Play Video Intent

Viewed 227004

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.

19 Answers

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.

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

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

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

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