youtube_feature_image

Integrating YouTube Android Player API


In this tutorial, you will learn how to Integrate YouTube Android Player API in Android Application. The YouTube Android Player API enables you to incorporate video playback functionality into your Android applications. The API defines methods for loading and playing YouTube videos (and playlists) and for customizing and controlling the video playback experience. so lets begin..

Note: This Library requires YouTube Application on the Device and won’t work without it.

 

Prerequisites:

 

Download

The YouTube library acts as a way to hook into the functionality of the main YouTube app, and therefore won’t work without it. To be able to use the library your device must have version 4.2.16 of the mobile YouTube app (or higher) installed.

You will also (obviously) need to download the library file itself. For this tutorial we will be using version 1.2.2.

 

Registering your application

Before you can start using the library, you will need to register your application with Google and get a client Id. The client Id will be used throughout the entirety of this tutorial and you won’t get far without it. Google Developers have created a pretty good guide on registering your application. Once you have the key, create a new string resource called CLIENT_ID that you can use through the application.

 

YouTube Video Details

Since we need some YouTube videos, and by videos I mean video ID’s, but what exactly is the ID of a YouTube video?

Go to your favourite YouTube video and look at the URL, it should look something like this:

 

Everything that comes after ?v= is the ID of that video, in this case LXtf84iK7i8.

We can also get more details about this video in json via the below link.

http://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=<YOUR VIDEO ID HERE>&format=json
for example:

 

Integration

Extract the contents of the YouTube zip file you downloaded earlier and locate the libs folder. Contained within this folder is the YouTube library, simply copy the .jar file from this directory into your project’s lib directory, typically located at appname/app/libs/ and add this file in the build.gradle file.

Checking For YouTube App on the Device

As previously mentioned, the library depends on the main YouTube application so it’s necessary to check whether it exists on the device before we start trying to use it.

 

Getting Video Thumbnail and Title

Firstly we’ll focus on displaying thumbnails. The library provides a new view: YouTubeThumbnailView, which we’ll be using to display thumbnails of videos.

To load an image into a ThumbnailView, you need a ThumbnailLoader. The job of the Loader is to take the ID of the video and load the Thumbnail for that video. When you call .initialize() on the ThumbnailView you provide the developer key you got earlier, and a callback listener. The two callback methods are used to associate a Loader with a ThumbnailView.

youtube

Playing YouTube Video

Using YouTubeIntents

One of the easiest ways to open a YouTube video is to simply direct the user to the actual YouTube app. This functionality hasn’t always been available so it’s necessary to check if the YouTube app is capable of handling an intent.  To do this we’re using a utility class called YouTubeIntents, specifically a method calledcanResolvePlayVideoIntent. If this method returns true then we know we can use YouTubeIntents to create our intent and the YouTube app knows how to handle it.

 

Using StandalonePlayer

This method is just as easy as the previous, but doesn’t require the user to leave your app. For most people, this will be the best way of playing videos.

youtube_standalone

Using  YouTubeFragment

Like any Fragment, the YouTubeFragment can be hosted by an Activity.

youtube_fragment

Using YouTubeBaseActivity

The YouTubePlayerView is a widget you can place in your XML layout or create an instance of in code much like we’ve been doing with the YouTubePlayerFragment.

youtube_activity

Customizing YouTubeBaseActivity

To get Lightbox mode on the video, use YouTubePlayerView along with the YouTubeBaseActivity. I use Semi Transparent background color have a cinematic-esque appearance on the video.

youtube_custom

 

Complete Source Code available at here.


About Durga Chiranjeevi

I’m a normal guy, engineer by education who is passionate about Programming and Internet. An android enthusiast, now moving for cross platforms and game development.