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:
- Download the latest library bundle from this download page.
- Unzip the downloaded file.
- Navigate for libs folder in the unzipped folder.
- Copy the YouTubeAndroidPlayerApi.jar file in your project’s libs folder.
- Now navigate to this folder in Android Studio.
- 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:
Intent intent = YouTubeIntents.createUserIntent(this, "PitbullVEVO"); startActivity(intent);
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 :
- createPlayVideoIntent(Context context, String videoId) – This is used for playing a video in YouTube app.
- createPlayPlaylistIntent(Context context, String playlistId) – This is used for playing a playlist in YouTube app.
- 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.
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:
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.
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:
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+.