在 Android 应用中播放 YouTube 视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11550797/
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
Playing youtube video in Android app
提问by SundayMonday
In my Android app I'd like the user to tap an image once, have a youtube video play automatically and when the video is done the user is immediately returned to the app. What's the best way to do this in Android?
在我的 Android 应用程序中,我希望用户点击一次图像,自动播放 youtube 视频,视频完成后,用户会立即返回到应用程序。在 Android 中执行此操作的最佳方法是什么?
I tried using intents. This works in that the video comes up on what I think is a youtube web page. However playing the video requires another tap. I'd like to avoid this if possible.
我尝试使用意图。这是因为视频出现在我认为是 youtube 网页上。但是播放视频需要再次点击。如果可能的话,我想避免这种情况。
I tried the whole MediaPlayer, prepareAsync, setOnPreparedListener and never got it to work. For some reason onPrepared was never called. No exceptions were thrown. I'm using the emulator to test and I'm new to Android so I'm not sure if the behavior will be different on physical devices.
我尝试了整个 MediaPlayer、prepareAsync、setOnPreparedListener,但从未让它工作。出于某种原因,从未调用过 onPrepared。没有抛出异常。我正在使用模拟器进行测试,而且我是 Android 新手,所以我不确定物理设备上的行为是否会有所不同。
I got this working well on iOS by getting creative with webviews. I'm hoping it's more straightforward on Android. The docssure make it sound straight forward.
通过使用 webviews 获得创意,我在 iOS 上运行良好。我希望它在 Android 上更简单。这些文档确实让它听起来很直接。
Cheers!
干杯!
回答by mportuesisf
Update:Everything below is still correct, but the official YouTube API for Androidis now available.
更新:下面的所有内容仍然正确,但适用于 Android的官方 YouTube API现在可用。
By far, the easiest way to play a YouTube video on Android is to simply fire an Intent to launch the native Android YouTube app. Of course, this will fail if you are not on a certified Google device, that doesn't have the complement of Google apps. (The Kindle Fire is probably the biggest example of such a device). The problem with this approach is that the user will not automatically wind up back at your app when the video finishes; they have to press the Back button, and at this point you've probably lost them.
到目前为止,在 Android 上播放 YouTube 视频的最简单方法是简单地触发 Intent 以启动本机 Android YouTube 应用程序。当然,如果您使用的不是经过认证的 Google 设备(没有 Google 应用程序的补充),这将失败。(Kindle Fire 可能是此类设备的最大例子)。这种方法的问题在于,当视频结束时,用户不会自动回到你的应用程序;他们必须按“返回”按钮,此时您可能已经失去了它们。
As a second option, you can use the MediaPlayer API to play YouTube videos. But there are three caveats with this approach:
作为第二种选择,您可以使用 MediaPlayer API 来播放 YouTube 视频。但是这种方法有三个注意事项:
1) You need to make a call to YouTube's GData webservice API, passing it the ID of the video. You'll get back a ton of metadata, along with it the RTSP URL that you should pass to MediaPlayer to play back an H.264-encoded stream. This is probably the reason why your attempt to use MediaPlayer failed; you probably weren't using the correct URL to stream.
1) 您需要调用 YouTube 的 GData 网络服务 API,将视频的 ID 传递给它。您将获得大量元数据,以及您应该传递给 MediaPlayer 以播放 H.264 编码流的 RTSP URL。这可能是您尝试使用 MediaPlayer 失败的原因;您可能没有使用正确的 URL 进行流式传输。
2) The GData/MediaPlayer approach will only play back low-resolution content (176x144 or similar). This is a deliberate decision on the part of YouTube, to prevent theft of content. Of course, this doesn't provide a very satisfactory experience. There are back-door hacks to get higher resolution streams, but they aren't supported on all releases of Android and using them is a violation of YouTube's terms of service.
2) GData/MediaPlayer 方法将仅播放低分辨率内容(176x144 或类似内容)。这是 YouTube 为防止内容被盗而做出的慎重决定。当然,这并不能提供非常令人满意的体验。有一些后门黑客可以获得更高分辨率的流,但并非所有版本的 Android 都支持它们,使用它们违反了 YouTube 的服务条款。
3) The RTSP streams can be blocked by some internal networks/firewalls, so this approach may not work for all users.
3) 某些内部网络/防火墙可能会阻止 RTSP 流,因此此方法可能不适用于所有用户。
The third option is to embed a WebView in your application. There two approaches you can take here:
第三个选项是在您的应用程序中嵌入一个 WebView。您可以在这里采取两种方法:
1) You can embed a Flash object and run the standard desktop Flash player for YouTube. You can even use the Javascript API to control the player, and relay events back to the native Android app. This approach works well, but unfortunately Flash is being deprecated on the Android platform, and will not work for Android 4.1 and later.
1) 您可以嵌入 Flash 对象并运行 YouTube 的标准桌面 Flash 播放器。您甚至可以使用 Javascript API 来控制播放器,并将事件中继回原生 Android 应用程序。这种方法效果很好,但不幸的是 Flash 在 Android 平台上已被弃用,不适用于 Android 4.1 及更高版本。
2) You can embed a <video>
tag to play YouTube via HTML5. Support for this varies between various releases of Android. It works well on Android 4.0 and later; earlier releases have somewhat spotty HTML5 <video>
support. So, depending upon what releases of Android your application must support, you can take a hybrid approach of embedding HTML5 on Android 4.x or later, and Flash for all earlier versions of Android.
2) 您可以嵌入<video>
标签以通过 HTML5 播放 YouTube。对此的支持因 Android 的不同版本而异。它在 Android 4.0 及更高版本上运行良好;早期版本对 HTML5 的<video>
支持有些参差不齐。因此,根据您的应用程序必须支持的 Android 版本,您可以采用混合方法,在 Android 4.x 或更高版本上嵌入 HTML5,在所有早期版本的 Android 上嵌入 Flash。
There are several threads here on StackOverflow about using HTML5 to play YouTube video; none of them really describe the entire process you must follow in one place. Here's links to a few of them:
StackOverflow 上有几个关于使用 HTML5 播放 YouTube 视频的主题;它们都没有真正描述您必须在一个地方遵循的整个过程。以下是其中一些的链接:
Android - How to play Youtube video in WebView?
Android - 如何在 WebView 中播放 Youtube 视频?
How to embed a YouTube clip in a WebView on Android
如何在 Android 上的 WebView 中嵌入 YouTube 剪辑
Play Youtube HTML5 embedded Video in Android WebView
在 Android WebView 中播放 Youtube HTML5 内嵌视频
All of this will get dramatically easier in the weeks/months to come; at Google I/O 2012, they presented/demoed a new YouTube API for Android that will support direct embedding of YouTube content in your application, with full support back to Android 2.2 (about 95% of the Android userbase as of this writing). It can't arrive fast enough.
在接下来的几周/几个月里,所有这一切都将变得容易得多;在 2012 年 Google I/O 大会上,他们展示/演示了一个适用于 Android 的新 YouTube API,它将支持在您的应用程序中直接嵌入 YouTube 内容,并完全支持 Android 2.2(截至撰写本文时,约有 95% 的 Android 用户群)。它不能足够快地到达。