播放YouTube视频的Andr​​oid应用 [英] Playing youtube video in Android app

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

问题描述

在我的Andr​​oid应用程序,我想挖掘一个图像,一旦用户,将自动拥有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,挂线preparedListener,从来没有得到它的工作。出于某种原因,在prepared从来没有叫。没有异常被抛出。我使用的模拟器测试,我是新来的Andr​​oid,所以我不知道,如果行为将在物理设备上的不同。

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.

我得到这个工作以及iOS上通过获得与webviews创意。我希望这是在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视频的最简单的方法就是激活一个意向,推出原生的Andr​​oid版YouTube应用。当然,如果你不是一个谷歌认证的设备,不具有谷歌应用程序的补上这将失败。 (烧火可能是这种装置中,最典型的例子)。这种方法的问题是,用户将不会自动卷起回到你的应用程序,当视频饰面;他们要preSS后退按钮,并在这一点上,你可能已经失去了他们。

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 Web服务的API,它传递视频的ID。你会得到一吨的元数据,以及它,你应该传递给MediaPlayer的是,RTSP URL播放的H.264-CN codeD流。这可能是原因,您尝试使用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的方法,只会播放低分辨率内容(176×144或类似)。这是一个深思熟虑的决定在YouTube上的部分,以内容prevent盗窃。当然,这不提供非常满意的体验。有后门黑客以获得更高的分辨率流,但不支持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对象和运行标准桌面Flash播放器使用YouTube。你甚至可以使用JavaScript API来控制播放器,以及接力项目回原生的Andr​​oid应用程序。这种方法效果很好,但不幸的是闪光灯正在德pcated在Android平台上$ P $,并且不会为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 <视频> 支持。因此,根据什么样的Andr​​oid版本的应用程序必须支持,您可以在Android 4.x或更高版本嵌入HTML5的混合方法,和Flash的所有早期版本的Andr​​oid。

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:

<一个href="http://stackoverflow.com/questions/9565533/android-how-to-play-youtube-video-in-webview?rq=1">Android - 如何在web视图播放YouTube视频

<一个href="http://stackoverflow.com/questions/3458765/how-to-embed-a-youtube-clip-in-a-webview-on-android">How嵌入在Android上一个的WebView一个YouTube视频

How to embed a YouTube clip in a WebView on Android

<一个href="http://stackoverflow.com/questions/6323169/play-youtube-html5-embedded-video-in-android-webview">Play的Youtube HTML5视频嵌入在Android中的WebView

这一切都将获得在未来几周/几个月来要方便得多;在谷歌I / O 2012年,他们presented /全世界展示一个新的YouTube API为Android将支持YouTube内容直接嵌入在应用程序,全面支持背到Android 2.2(约95%的Andr​​oid用户群,作为本写作)。它无法到达足够快的。

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.

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

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