8

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?

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.

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.

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.

Cheers!

SundayMonday
  • 19,147
  • 29
  • 100
  • 154

1 Answers1

29

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

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.

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

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) 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) The RTSP streams can be blocked by some internal networks/firewalls, so this approach may not work for all users.

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

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) 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.

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 - How to play Youtube video in WebView?

How to embed a YouTube clip in a WebView on Android

Play Youtube HTML5 embedded Video in Android WebView

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.

Community
  • 1
  • 1
mportuesisf
  • 5,587
  • 2
  • 33
  • 26
  • The upcoming API sounds great! Thanks for the info. – SundayMonday Jul 18 '12 at 22:29
  • I'd be interested in pointers towards those back-door hacks you mention. And I'd like to add that some devices are unable to see a download for Adobe Flash on Google Play, effectively ruining the hybrid strategy of relying on YouTube's Flash player for Froyo, Gingerbread, Honeycomb. – Giulio Piancastelli Jul 18 '12 at 22:29
  • @Giuilo - In our app, we fall back to the GData/MediaPlayer API if Flash is not present. It's better than a black screen. – mportuesisf Jul 18 '12 at 22:31
  • 2
    @SundayMonday - here's the video of the session where Google presents their new YouTube API: http://www.youtube.com/watch?v=3WFsx-u-q3Y&list=PL4C6BCDE45E05F49E&index=6&feature=plpp_video – mportuesisf Jul 18 '12 at 22:34
  • 1
    @Giulio - here's the code that shows off the "backdoor" hack: http://keyeslabs.com/joomla/blogs/i-think-im-becoming-an-android/51-polish-your-app-free-embeddable-android-youtube-activity It uses the GData/MediaPlayer API approach, and is a good piece of example code for that technique. The back-door aspect is that it grabs format code 18, which is the URL for the higher-resolution version of the stream. I think the problem with this approach, last time I tried it (long time ago), was that it worked on Android 2.1 and broke on Android 2.2. Your mileage may vary today. – mportuesisf Jul 18 '12 at 22:39
  • @mportuesisf Thanks a lot! I see that the code uses YouTube URLs with `get_video`, which is now unsupported. A new version of the code (that can be found on Google Code at http://code.google.com/p/android-youtube-player) uses `get_video_info` to get away with the format trick, but I fear that even support for those URLs could be dropped without notice. While waiting for the easy API to be deployed, however, if the code works, could be a nice addition between Flash player and GData/`VideoView`/`MediaPlayer`, the quality of the latter probably being just a little bit too low for my requirements. – Giulio Piancastelli Jul 18 '12 at 23:06
  • @Guilio - thanks for the pointer to the updated code example. I'm probably going to press on with the hybrid GData/Flash/HTML 5 approach, since I'm already pretty heavily invested in it. Then chuck the whole thing when Google delivers their real API. – mportuesisf Jul 18 '12 at 23:59