XAMARIN - 从 youtube 添加视频 [英] XAMARIN - add video from youtube

查看:23
本文介绍了XAMARIN - 从 youtube 添加视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索有关如何从youtube添加视频的信息,例如:我想从一些 youtube 链接添加视频.我认为它应该在 webview 中,但我需要一些详细信息,因为我找不到有关我的问题的任何信息.

i search information how to add video from youtube, example: I want add video from some youtube link. I think that it should be in webview, but i need some details, because i can't find any information about my problem.

推荐答案

更新:下面的一切仍然正确,但 适用于 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.

感谢 @mportuesisf 这个精彩的回答.

Thanks @mportuesisf for this wonderful answer.

这篇关于XAMARIN - 从 youtube 添加视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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