强制视频在 Android 上的 Youtube 应用程序中打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11114341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Force video to open in Youtube app on Android
提问by tba
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.
我有一个链接到 YouTube 视频的移动网站。在 Android 上,单击此链接会弹出一个对话框,要求用户“使用”他们的浏览器或 Youtube 应用程序“完成操作”。
Is there a way to bypass this screen and just play the video in the Youtube app? (For example with a youtube:// URL.)
有没有办法绕过这个屏幕,只在 Youtube 应用程序中播放视频?(例如使用 youtube:// URL。)
Thanks!
谢谢!
回答by Tim
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
id 是 url 中问号后的标识符。例如: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);
......
......
回答by sanath_p
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);
}
回答by Casper
Try to use a JavaScript redirection like the following:
尝试使用如下所示的 JavaScript 重定向:
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.
如果 Youtube 应用被禁用,超时功能会将您重定向到 Play 商店中的 youtube 应用,让您启用该应用。第二个重定向将弹出并在 Android Youtube 应用程序上播放 youtube 视频。
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.
如果您已经在超时间隔内切换到 YouTube 应用,则不会调用超时功能,您也不会切换到 Play 商店,而是停留在 YouTube 应用中。