YouTube Player Integration in Android

24 / Jun / 2016 by Vaibhav Chahal 0 comments

YouTube allows you to play it’s video in your app using the youtube player. It also provides data APIs to put like, dislike, or rate comments on a video.

To integrate youtube player you need API key from google console and you can get it using following steps:

1. Get SHA1 fingerprint first by executing “keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android” command on terminal.

2. Now visit the Developer console and create credential -> API key under the credentials section.


3. Get Android API key by passing your package name and SHA1 fingerprint that you got in step 1.

Now download the zip file available at this link Download Youtube Player.
Extract youtubeAndroidplayer.jar from the zip folder and put that in the libs folder of your project.

Open your activity class (let’s say, the activity should extend from YouTubeBaseActivity, which will be present in YouTubeAndroidPlayerApi.jar. This activity also contains few initialization listener methods to know the status of the youtube player.

public class YouTubePlayerActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

private static final int RECOVERY_REQUEST = 1;
private YouTubePlayerView youTubeView;
VideoView videoView;
private String videoId;

protected void onCreate(Bundle savedInstanceState) {

videoId = "fhWaJi1Hsfo";

youTubeView = (YouTubePlayerView) findViewById(;
youTubeView.initialize(Config.YOUTUBE_API_KEY, this);

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(Config.YOUTUBE_API_KEY, this);

private YouTubePlayer.Provider getYouTubePlayerProvider() {
return youTubeView;

public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);

public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);

public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
if (!wasRestored) {
youTubePlayer.cueVideo(videoId); // Plays

public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {

if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_REQUEST).show();
} else {
            String error = String.format(getString(R.string.player_error), errorReason.toString());
            Toast.makeText(this, error, Toast.LENGTH_LONG).show();



In the above activity, you need to replace YOUTUBE_API_KEY with your API key you got from your console and videoId with the id of the video you want to play in your player.

In your XML file put YouTubePlayerView to play video:


Also, you will need internet permission in your manifest file

uses-permission android:name="android.permission.INTERNET";

After running the app, your youtube player will look like:


To like and dislike a video you need to call the following ‘GET’ youtube APIs.

To like a video-

To dislike a video-

Further, to get video statistics such as total number of likes, dislikes, or view count:



Leave a comment -