在 Android 应用中播放 YouTube 视频 [英] Playing youtube video in Android app

查看:42
本文介绍了在 Android 应用中播放 YouTube 视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Android 应用程序中,我希望用户点击一次图像,自动播放 youtube 视频,视频播放完毕后,用户会立即返回到应用程序.在 Android 中执行此操作的最佳方法是什么?

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?

我尝试使用意图.这是因为视频出现在我认为是 youtube 网页上.但是播放视频需要再次点击.如果可能,我想避免这种情况.

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.

我尝试了整个 MediaPlayer、prepareAsync、setOnPreparedListener,但从未让它工作.出于某种原因,从未调用过 onPrepared.没有抛出异常.我正在使用模拟器进行测试,而且我是 Android 新手,所以我不确定物理设备上的行为是否会有所不同.

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.

我通过使用 webviews 获得创意,使其在 iOS 上运行良好.我希望它在 Android 上更简单.文档 确保它听起来很直接.

I got this working well on iOS by getting creative with webviews. I'm hoping it's more straightforward on Android. The docs sure make it sound straight forward.

干杯!

推荐答案

更新:下面的一切仍然正确,但 适用于 Android 的官方 YouTube API 现已推出.

Update: Everything below is still correct, but the official YouTube API for Android is now available.

到目前为止,在 Android 上播放 YouTube 视频的最简单方法是简单地触发 Intent 以启动本机 Android YouTube 应用.当然,如果您使用的不是经过认证的 Google 设备(没有 Google 应用程序的补充),这将失败.(Kindle Fire 可能是此类设备的最大例子).这种方法的问题在于,当视频结束时,用户不会自动回到你的应用程序;他们必须按返回"按钮,此时您可能已经失去了他们.

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.

作为第二种选择,您可以使用 MediaPlayer API 来播放 YouTube 视频.但是这种方法有三个注意事项:

As a second option, you can use the MediaPlayer API to play YouTube videos. But there are three caveats with this approach:

1) 您需要调用 YouTube 的 GData 网络服务 API,将视频的 ID 传递给它.您将获得大量元数据,以及您应该传递给 MediaPlayer 以播放 H.264 编码流的 RTSP URL.这可能是您尝试使用 MediaPlayer 失败的原因;您可能没有使用正确的 URL 进行流式传输.

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.

2) GData/MediaPlayer 方法将仅播放低分辨率内容(176x144 或类似内容).这是 YouTube 为防止内容被盗而做出的慎重决定.当然,这并不能提供非常令人满意的体验.有一些后门黑客可以获得更高分辨率的流,但并非所有版本的 Android 都支持它们,使用它们违反了 YouTube 的服务条款.

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.

3) 某些内部网络/防火墙可能会阻止 RTSP 流,因此此方法可能不适用于所有用户.

3) The RTSP streams can be blocked by some internal networks/firewalls, so this approach may not work for all users.

第三个选项是在您的应用程序中嵌入一个 WebView.您可以在此处采用两种方法:

The third option is to embed a WebView in your application. There two approaches you can take here:

1) 您可以嵌入 Flash 对象并运行 YouTube 的标准桌面 Flash 播放器.您甚至可以使用 Javascript API 来控制播放器,并将事件中继回原生 Android 应用程序.这种方法效果很好,但遗憾的是 Flash 在 Android 平台上已被弃用,不适用于 Android 4.1 及更高版本.

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.

2) 您可以嵌入 标签以通过 HTML5 播放 YouTube.对此的支持因 Android 的不同版本而异.它在 Android 4.0 及更高版本上运行良好;早期版本对 HTML5 的支持有些参差不齐.因此,根据您的应用程序必须支持的 Android 版本,您可以采用混合方法,在 Android 4.x 或更高版本上嵌入 HTML5,在所有早期版本的 Android 上嵌入 Flash.

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.

StackOverflow 上有几个关于使用 HTML5 播放 YouTube 视频的主题;它们都没有真正描述您必须在一个地方遵循的整个过程.以下是其中一些的链接:

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:

Android - 如何在 WebView 中播放 Youtube 视频?

如何嵌入 YouTube 剪辑Android 上的 WebView

在 Android WebView 中播放 YouTube HTML5 嵌入视频

在接下来的数周/数月内,所有这一切都将变得容易得多;在 2012 年 Google I/O 大会上,他们展示/演示了一个适用于 Android 的新 YouTube API,该 API 将支持在您的应用程序中直接嵌入 YouTube 内容,并完全支持 Android 2.2(截至撰写本文时,约有 95% 的 Android 用户群).它不能足够快地到达.

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.

这篇关于在 Android 应用中播放 YouTube 视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆