Android YouTube API Tutorial


Android YouTube API

Well well its official, on 10th May 2013 Google has finally released YouTube API for android. You might be wondering what difference does it make, earlier too we were able to play YouTube videos on Android. The difference in this API is the way we are accessing it. Earlier to play a YouTube video on an Android device in an app we had to fire an Intent. Further with this Intent the android’s YouTube player App was launched and the video was played over there. But now with this new Android YouTube API we would be able to play HD YouTube videos in our Android app, i.e. without an intent. Also we would have full control over the playback.

Earlier I wrote a tutorial to play YouTube videos on android, in which the only limitation was, that we could not stream HD YouTube videos. Hence I had to write this Android YouTube API Tutorial. With this new API we can we can seamlessly stream high quality YouTube videos and also maintain their state while screen is rotated i.e. resuming video from where it stopped. Although if you would like to give a read to my tutorials on streaming YouTube video on Android please have a look at the following links:

  1. Android VideoView Example
  2. Android YouTube Player API Tutorial

To start off with Android YouTube Player API there are certain steps that are needed to be performed. But first let me introduce you to the concept through which this player works. Android YouTube Player can be used in any app by importing a library in to that app’s project, but by doing this it does not mean that you have every thing. Android YouTube Player API works along with the device’s native YouTube Player. Means even after importing a library we would require an android device which has YouTube app installed (min ver 4.2.16). In reality this API library which we imported in to our app project, will communicate with a service of Android YouTube app which is bundled with the OS. To begin the tutorial lets follow these steps:

Step 1 : Include a jar file in your project

Download Android YouTube Player API bundle from here. Unzip this file and follow the steps below to include it in your app:

  1. Copy the jar file from libs folder to the the libs folder of your project.
  2. Right click on your project, Build Path -> Configure Build Path -> Libraries Tab
  3. Add Jars -> Select Your project’s libs folder -> Select YouTubeAndroidPlayerAPI.jar ->Ok
  4. Order and Export Tab -> Check YouTubeAndroidPlayerAPI.jar – Ok

Step 2 : Create an API Key for Android YouTube Player

To access the Android YouTube Player API on device we need to create an API key from Google API developer console. The steps to do so are:

  1. Login to API console
  2. Create a Project -> Goto Services -> Turn on the switch for Google YouTube Data API v3
  3. Goto API Access -> Create new Android Key-> In popup enter one SHA1 certificate fingerprint and package name (separated by a semicolon) per line.
  4. To get SH1 certificate of your app open command prompt, navigate to your Java bin folder and enter this command:
  5. In the above command just replace the keystore user path with your system’s user path, and this should work.
  6. From here copy Sh1 certificate and also copy package name of your spp from eclipse.
  7. Now enter these values in the API Console popup in the desired format, you key will be generated.

Step 3 : Write code for Android YouTube Player API

Now that we have all the prerequisites in place lets start with the app manifest first:

Lets define the layout which we are going to use for Android YouTube API:

Have a look at the above layout, here the com.google.android.youtube.player.YouTubePlayerView is being used to display the YouTube Video. Next lets have a look at the code through which Android YouTube Player API can be used:

To make the above Android YouTube API example work you need to specify your developer key. In above example first onCreate method is called, in this method YouTubePlayerView is initialized by YouTubePlayerView.initialize() method. Further if the initialization is successful, onInitializationSuccess() method is called. In this method one can specify the video that needs to be played. Also if you notice I have declared the player variable as a global variable by the name of YPlayer. With the help of this variable we would be able to access the Android YouTube Player API throughout the Activity, and perform play, pause, seek and various other functions.

After performing all the above steps, your app would look something like the screenshots above. An interesting feature of using this Android YouTube Player API, is that we don’t have to do any sort of programming to make the video run in full screen mode. Also no effort is required to stream HD YouTube videos. Another interesting feature is that while playback if the screen is rotated, the video playback starts exactly from where it stopped, ain’t that great as no amount of coding is to be done for this feature too. Hope this Android YouTube API Tutorial helped you in some way. If it did or you simply liked it, please like and share it with your friends on Google+ and Facebook.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.


Leave a comment

Your email address will not be published. Required fields are marked *

34 thoughts on “Android YouTube API Tutorial