Embed Video in your Android Application Using YouTube Android Player API v1.2.1

With YouTube Android Player API, you can embed videos and playlists into your application. User gets playback and controlling experience much similar Official YouTube Android Application. User can control loading and playing of embedded YouTube video. Playing, cueing and loading can be controlled programmatically as well. Event listeners api’s with support of events like open fullscreen or player loading etc gives you complete control that you would desire of.

YouTube Android Player Api client library is dependent on YouTube Application, it interacts with services part of YouTube application for video playback.

 

 

 

Step 1. Registering application with Google Developers Console

1.1 Go to Google Developers Console, Create a new project if none exists or select an existing project.

1.2 Navigate to APIs & auth -> APIs in left sidebar, browse for YouTube Data API v3 and turn on its status accepting terms of services.

 

YouTubePlayerAPI_Step1

 

1.3 Generate SHA1 fingerprint using java keytool utility

For getting API key, we need to generate SHA-1 fingerprint using java keytool. Open your command prompt and execute the below mentioned command to generate SHA-1 fingerprint. If keytool is not recognized, you can find it in bin directory of installed jre. In my case C:\Program Files\Java\jre7\bin.

keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

 

YouTubePlayerAPI_SHA1_keytool

 

From output note down SHA1 output, “FD:0E:04:E9:99:28:B9:3D:E7:AC:75:AF:6E:2B:F6:E7:CD:EE:CA:96″ in my case.

 

1.4 Generate API Key

Navigate to APIs & auth -> credentials in left sidebar, select Create New Key and opt for Android key.

 

YouTubePlayerAPI_AndroidKey

 

Enter your SHA1 fingerprint and application package name sperated by semi-colon(;) and select create. It will generate API key for your application. Note down API key for later usage. API Key generated in my case is AIzaSyBcTwxVG7eYC314bzw1D28XwFi5xe6DT7k.

 

YouTubePlayerAPI_AndroidKeyStep3

Step 2. Developing Sample Application using YouTube Android Player API

2.1 Create new Android Application Project in Eclipse with package name.

 

YouTubeAPI_Demo

 

2.2 Download latest version 1.2.1 of YouTube Android Player API and extract downloaded file.

2.3 From extracted files, copy YouTubeAndroidPlayerApi.jar from libs folder to add it to libs folder of your android application.

2.4 Preparing Application Manifest File

Add android.permission.INTERNET permission to access Internet. We will have just one activity in our application which will embed YouTube video.

2.5 Preparing Layout File

In layout file, add com.google.android.youtube.player.YouTubePlayerView view which will used for video playback. Modify your launcher activity layout as below.

2.6 Preparing Launcher Activity

For embedding YouTube video in an activity. We must take care of two points.
1. In order to incorporate YouTubePlayerView views in your UI, you must extend YouTubeBaseActivity.
2. Implement YouTubePlayer.OnInitializedListener for listening to intialization callback of success or failure. We set this callback while intializing YouTubePlayerView.

In case of successful intialization of YouTubePlayerView, we get object of YouTubePlayer which can be used to control player programmatically to cue videos and control playback etc. For failure case, we check if it is recoverable error then we reinitialize YouTubePlayerView.

For listening to fullscreen change events, we can use YouTubePlayer.OnFullscreenListener

For listening to playback events like buffering, play, pause, seek and stop, you can use YouTubePlayer.PlaybackEventListener.

For listening to player state change events, implement YouTubePlayer.PlayerStateChangeListener

Our main activity code will be as below. For cueing videos in YouTubeVideoPlayer, we need to provide video id for it. I have taked video id for song “Let It Go” from Frozen movie. On intialization of view, we will cue this video. For keeping it simple, i have just used initialization listener.

2.7 Build and Run your Application

If you have completed all steps above carefully, build and run your application. You will see YouTube video embedded in your application.

You may also like...