Takeaway: Explore how to embed YouTube videos in your Android apps using Google’s new API.
While I’m an admitted Android fanboy, I am not above acknowledging areas where the platform could use improvement. In the past, one of these shortcomings in Android’s offerings for developers has been integration with YouTube. This is a particularly painful point for us fanboys because A) both Android and YouTube are Google projects, and B) my colleagues who work primarily on iOS have had a relatively consistent and pleasant integration experience with YouTube for some time now using iPhone’s standard webview component.
Luckily, the engineers at Google have been working hard behind the scenes. Now at version 3.0 and free of the “experimental” label, Google hails its YouTube API to be ready for prime time. The documentation promises: “…the API lets your application provide a full-fledged YouTube experience that includes search and discovery, content creation, video playback, account management, and viewer statistics.”
I don’t know about you, but I’m ready for a test drive!
1. You will need a mobile device running version 4.2.16 or better of the official YouTube app. That’s right, the underlying service used by the Android YouTube Client API is distributed along with the actual YouTube application for Android. The lack of separation was a little disheartening to me, but Google claims the necessary scaffolding should be available for all devices running Android 2.2 or higher, and bundling the service with the app reduces overall footprint on the device.
2. Next you need an API key. It’s free, but you need your Google account to get it. If you want to run the sample demo that follows, you will need to get an API key (since I don’t plan on sharing mine with the entire Internet). You can find detailed instructions here. (If you don’t want to take the time to submit an SHA 1 Fingerprint Certificate, you can leave the field blank for the time being, and the API will accept all your calls. Just be sure to go back and restrict it to your own package/apps before releasing anything into the wild.)
3. After you register for an API key, you are ready to open Eclipse and start a new Android project. Remember to target at least Android 2.2.
4. Before going on we need to download the client library files. Unzip the compressed folder, locate the YouTubeAndroidPlayerApi.jar (Figure A), and add it to your project’s /libs folder.
5. Right-click the jar file, then go to Build Path | Configure Build Path. Click the Add JARs button (Figure B).
6. From the JAR Selection dialog, navigate to the recently copied jar file and select it (Figure C).
7. Next we need to open the project’s AndroidManifest.xml file and add the Internet permission.
As you can see, it is the setup, not the actual code, that represents the majority of the work. Despite the documentation suggesting the player view would work on the Android emulator, I was not able to find a platform where that was the case even after updating all my tools and SDKs to the latest and greatest releases (Figure D).
Fortunately, the application did seem to work well on my Nexus 4 hardware (Figure E).
My final thoughts
Unfortunately, the API still seems a bit complicated to me — less so on the programming side and more on the setup. However, it appears to work on my device (if not the emulator), so if further testing shows it to work consistently across the Android hardware landscape, I am willing to invest the extra effort.
For those of you who’d like to try this project on your own devices, you can download it here. Just remember, you must register for your developer API code and insert it into the MainActivity.java file before the app will work.