tba
tba

Reputation: 6571

Force video to open in Youtube app on Android

I have a mobile website that links to a youtube video. On Android, clicking on this link brings up a dialog asking the user to "Complete action using" their browser or the Youtube app.

Is there a way to bypass this screen and just play the video in the Youtube app? (For example with a youtube:// URL.)

Thanks!

Upvotes: 15

Views: 28444

Answers (3)

Joshua LI
Joshua LI

Reputation: 4733

Try to use a JavaScript redirection like the following:

window.location = "vnd.youtube://the.youtube.video.url";

More comprehensively:

if( /Android/i.test(navigator.userAgent ) ) {
    // If the user is using an Android device.
    setTimeout(function () { window.location = "market://details?id=com.google.android.youtube"; }, 25);
    window.location = "vnd.youtube://www.youtube.com/watch?v=yourVideoId";
}

If Youtube app is disabled, the timeout function will redirect you to the youtube app on the play store to let you enable the app. The second redirect will pop up and play the youtube video on Android Youtube app.

If you are already switched to the YouTube app within the timeout interval, the timeout function will not be called and you are not switched to the play store but stay in the YouTube app.

Upvotes: 4

sanath_p
sanath_p

Reputation: 2218

The best way

try {

     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + id));
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(intent);

} catch (ActivityNotFoundException e) {

    // youtube is not installed.Will be opened in other available apps

    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://youtube.com/watch?v=" + id));
    startActivity(i);
}

Upvotes: 8

Tim
Tim

Reputation: 6712

Here's how you can do that:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + id));
startActivity(intent);

The id is the identifier after the questionmark in the url. For example: youtube.com/watch?v=ID

Another way is:

Intent videoIntent = new Intent(Intent.ACTION_VIEW);
videoIntent.setData(url);
videoIntent.setClassName("com.google.android.youtube", "com.google.android.youtube.WatchActivity");
startActivity(videoIntent);

......

Upvotes: 22

Related Questions