Android YouTube API Example For Channels


Photo Credit: M Anima - YouTube Android API Channels

A lot of people have been asking me how to integrate YouTube channels into an Android app. The answer is, by usage of YouTube data API and YouTube Android player API. Since as of now YouTube Android player does not provide any mechanism to access channels, we need to use the YouTube Data API with it to access the list of channels. Many of us don’t know that, YouTube only provides a player API for Android. As such there is no support for channels in that API. Although YouTube channels functionality is implemented in their official app. Which makes me think, if they can do it, we can do it too.

But first lets understand what YouTube channels are?

Speaking of channels, they are always associated with YouTube users. Actually they are same, users are custom generated URLs, which YouTube channel owners can generate through their account. Reason being channels have a long system generated ID attached to them, which becomes a little difficult for them to distribute. In this YouTube Android API channels tutorial, I would show how videos can be shown from both URLs.

As of now there are two ways through which we can show a YouTube channel in an Android App. But first you may need to add a library for YouTube Android Player API:

  1. Download the latest library bundle from this download page.
  2. Unzip the downloaded file.
  3. Navigate for libs folder in the unzipped folder.
  4. Copy the YouTubeAndroidPlayerApi.jar file in your project’s libs folder.
  5. Now navigate to this folder in Android Studio.
  6. Right click on the YouTubeAndroidPlayerApi.jar file and select the “Add as library”.

And you’re all set, to play YouTube channels in your Android app. Now lets have a look at the two methods through which YouTube Android player API can show channels:

1) Android YouTube Intents

YouTube player API gives us a list of methods through which we can open specific screens of the official YouTube Android app. These methods can be accessed through the YouTubeIntents class in the API. Android YouTube Player API gives us a specific method  createUserIntent  through which we can open user screen, in the official YouTube app. Code sample:

This can quickly solve your problem of opening YouTube users, without getting into the complexities of channel ID parsing.

Other than this Android YouTube player API provides many more static methods which can be used to send various intents to the YouTube app. Some of the basic types are :

  1. createPlayVideoIntent(Context context, String videoId) – This is used for playing a video in YouTube app.
  2. createPlayPlaylistIntent(Context context, String playlistId) – This is used for playing a playlist in YouTube app.
  3. createUploadIntent(Context context, Uri videoUri) – This is used to open the upload video screen of YouTube Android app.

Recently in January 2015, an update to Android YouTube API was released, which made some additions in YouTubeIntents class. Now through canResolvePlayVideoIntentWithOptions method, we can check whether the installed YouTube app supports the fullscreen mode or not. For more details please refer to the YouTubeIntents class.

2) Android YouTube API Channels

With the help of above mentioned approach you can view YouTube users/channels in official app. But if you want to display channels in your own app, then you might have to a little more coding. As you might have understood by now, channel is not an inbuilt functionality of YouTube. Its a way, through which users can organize their videos. In short a channel/user is nothing but a collection of YouTube playlists.

To show playlists in your app from a username, you might have to make two YouTube Data API calls and then play the chosen playlist through Android YouTube Player API, integrated in your app.

1) YouTube channel list – In Android to get a YouTube channel ID, we have to make a YouTube Data API call for channel list. This API can list down all the channel IDs associated with specified username. To read more about this you can visit the YouTube Data API channel list page, or you can simply use the URL below to get a username’s channelId.

Please Note: To access the API please generate an API key form Google API Console, for steps please refer to Android YouTube player example.

This API call would return a JSON object, you may have to parse it and extract the channel id from it, for further usage. See the image below:

Android YouTube API Channel List

 

2) Android YouTube Data API Playlists – Now that we have a channelId, we are one step closer for displaying YouTube channels in Android app. Next we need to get a list of playlists for our channel ID. To get a list of playlists associated with a channel, we need to make a Playlists list API call. To read more about it please refer to the page YouTube Playlists List, or you can use the URL below to make the call.

Please Note: To access the API please generate an API key form Google API Console, for steps please refer to Android YouTube player example.

For e.g. use the channelId (highlighted in point 1 above) in this URL. It would return a JSON object containing a list of playlists. Here each playlist would have an object of its own with an ID associated with it. You may have to parse it for further usage. Please see the image below:

Android YouTube API Channel Playlist List

 

3) Android YouTube API Playlists – Luckily YouTube Player API for Android supports the playlists. But to play a playlist, first we need to implement a YouTube Player in our app. To learn more about how to implement a YouTube player in your app please have a look at this Android YouTube API example. To run playlists in the specified example just replace the  cueVideo  method line with:

Here the specified id is playlist id from the point 2.

This concludes the Android YouTube API Example For Channel, although who knows in future, maybe a new way is introduced to show YouTube channels on Android. As the Android YouTube API is in a very nascent stage. Hope this tutorial helped you. Stay connected with us through Facebook, Twitter and Google+.

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 *

4 thoughts on “Android YouTube API Example For Channels

  • Yunus

    Hi Mohit,
    I read your post. That’s really cool.
    But what will be the case if I have a list of videoIds (YouTube Video URLs) stored locally.
    Now in my screen I have two buttons, viz Previous and Next
    What I want is to load the next video from my Array (List) when user clicks the Next button and similarly to load the previous video when user clicks the Previous button.
    I know there is a method onInitializatioSuccess(…) in which, we are calling player.loadVideo(…)
    But I really don’t know how can I traverse the videos in my case.
    Any help would be greatly appreciated.
    Thanks

  • Sanjay

    Hey Mohit, this is a great article and very useful. I wanted to check if it is possible to fetch all the user reviews for the playlists from channel? Also, can we allow users to review for the youtube playlists from within our app?

  • Sagar

    Hey Mohit!.. I found this post really very helpful. I even tried to follow this code. I couldn’t be able to find my channel ID from this URL

    https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername={USERNAME}&key={YOUR_API_KEY}

    I hv put my username & APIKEy.. but what i got is this:
    {
    “error”: {
    “errors”: [
    {
    “domain”: “usageLimits”,
    “reason”: “ipRefererBlocked”,
    “message”: “The request did not specify any Android package name or signing-certificate fingerprint. Please ensure that the client is sending them or use the API Console to update your key restrictions.”,
    “extendedHelp”: “https://console.developers.google.com/apis/credentials?project=963911194273”
    }
    ],
    “code”: 403,
    “message”: “The request did not specify any Android package name or signing-certificate fingerprint. Please ensure that the client is sending them or use the API Console to update your key restrictions.”
    }
    }

    Plz help me out of this.