Android YouTube 应用播放视频意图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/574195/
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
Android YouTube app Play Video Intent
提问by Isaac Waller
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.
我创建了一个应用程序,您可以在其中下载适用于 Android 的 YouTube 视频。现在,我想要它,如果你在 YouTube 原生应用中播放视频,你也可以下载它。为此,我需要知道 YouTube 原生应用为了播放 YouTube 应用而推出的 Intent。
如果我的模拟器上有 YouTube 程序,我可以轻松地做到这一点,所以我的第一个问题是:
1. 我可以为我的模拟器下载 YouTube 应用程序,或者...
2. 用户选择视频时使用的意图是什么用于播放。
回答by Roger Garzon Nieto
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);
}
}
Note: Beware when you are using this method, YouTube may suspend your channel due to spam, this happened two times with me
注意:当您使用此方法时请注意,YouTube 可能会因垃圾邮件而暂停您的频道,这在我身上发生过两次
回答by emmby
This will work on a devicebut not the emulatorper Lemmy's answer.
根据Lemmy 的回答,这适用于设备,但不适用于模拟器。
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM")));
回答by Bibbity Bobbity Boo
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. '://'):
现在我已经做了一些更多的研究,看起来我只需要“vnd.youtube:VIDEO_ID”而不是冒号后面的两个斜杠(“:”与“://”):
http://it-ride.blogspot.com/2010/04/android-youtube-intent.html
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.
我在这里尝试了大部分建议,但它们并不能很好地处理所有引发异常的所谓“直接”方法。我会假设,使用我的方法,如果没有安装 YouTube 应用程序,操作系统有一个默认的后备位置,而不是使应用程序崩溃。该应用程序理论上只能在装有 YouTube 应用程序的设备上运行,因此这应该不是问题。
回答by Soubhab Pathak
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 ....
使用我的代码 .. 我可以使用此代码播放 youtube 视频 ......您只需要在“videoId”变量中提供 youtube 视频 ID ......
String videoId = "Fee5vbFLYM4";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+videoId));
intent.putExtra("VIDEO_ID", videoId);
startActivity(intent);
回答by Sana
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.
videoId
必须播放的 youtube 视频的视频 ID在哪里。此代码在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.
但基本上我们可以做的是检查启动 Youtube 应用程序时加载的活动,并相应地替换 packageName 和 className。
回答by Lemmy
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.
Youtube(和 Market 应用程序)只能与 Google 为 G1 和 G2 发布的特殊 ROM 一起使用。因此,不幸的是,您不能像在模拟器中使用的那样在 OpenSource-ROM 中运行它们。好吧,也许你可以,但不是以官方支持的方式。
回答by Warpzit
EDIT: The below implementation proved to have problems on at least some HTC devices (they crashed). For that reason I don't use setclassname and stick with the action chooser menu. I strongly discourage using my old implementation.
编辑:以下实现证明至少在某些 HTC 设备上存在问题(它们崩溃了)。出于这个原因,我不使用 setclassname 并坚持使用操作选择器菜单。我强烈反对使用我的旧实现。
Following is the old implementation:
以下是旧的实现:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(youtubelink));
if(Utility.isAppInstalled("com.google.android.youtube", getActivity())) {
intent.setClassName("com.google.android.youtube", "com.google.android.youtube.WatchActivity");
}
startActivity(intent);
Where Utility is my own personal utility class with following methode:
其中 Utility 是我自己的个人实用程序类,具有以下方法:
public static boolean isAppInstalled(String uri, Context context) {
PackageManager pm = context.getPackageManager();
boolean installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}
First I check if youtube is installed, if it is installed, I tell android which package I prefer to open my intent.
首先我检查 youtube 是否已安装,如果已安装,我告诉 android 我更喜欢打开我的意图的包。
回答by Isaac Waller
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) }
回答by Shardul
Replying to old question, just to inform you guys that package have changed, heres the update
回复老问题,只是为了通知大家包已经改变了,这里是更新
Intent videoClient = new Intent(Intent.ACTION_VIEW);
videoClient.setData("VALID YOUTUBE LINK WITH HTTP");
videoClient.setClassName("com.google.android.youtube", "com.google.android.youtube.WatchActivity");
startActivity(videoClient);
This works very well, but when you call normal Intent with ACTION_VIEW with valid youtube URL user gets the Activity selector anyways.
这非常有效,但是当您使用 ACTION_VIEW 使用有效的 youtube URL 调用普通 Intent 时,用户无论如何都会获取 Activity 选择器。
回答by Ahmad
The safest way to run videos on a different app is by first trying to resolve the package, in other words, check that the app is installed on the device. So if you want to run a video on youtube you'd do something like this:
在不同的应用程序上运行视频的最安全方法是首先尝试解析包,换句话说,检查该应用程序是否安装在设备上。因此,如果您想在 youtube 上播放视频,您可以执行以下操作:
public void playVideo(String key){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + key));
// Check if the youtube app exists on the device
if (intent.resolveActivity(getPackageManager()) == null) {
// If the youtube app doesn't exist, then use the browser
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=" + key));
}
startActivity(intent);
}