Force video to open in Youtube app on Android

2019-01-18 12:53发布

问题:

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!

回答1:

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

......



回答2:

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


回答3:

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.