Jack and Jill says this tutorial is retired. And we agree! This tutorial was published back in May 2014, since then lot has been changed; YouTubePlayerAPI is updated and Eclipse for Android development is history. Checkout our new YouTube Android Player API Example here.

In this tutorial we’ll see how to use YouTube Player API in android. YouTube player API provides an embedded view that supports with various playback controls.

1. Downloading YouTube Player API

Before you start writing some code, you’ll have to first download the YouTube Android Player API. Once downloaded import the jar into your project lib folder and add the jar to your build path.

Download YouTube Player API here

YouTube Android Player API

2. Register your app in Google developer console

  • Before you start using the Youtube player API for android, you have to register your application with Google developer console and get and API key, The API key is a unique value for your application. Do not disclose this to others.
  • Visit Google APIs Console and login with your Google account and create a new project
  • Once your project is created, got to API’s and enable YouTube data API V3
  • And then Select API Access in your API Console. You’ll find API key. This API key will be used to access YouTube Android Player APIs.

YouTube Android Player API2

3. Create a new Android Application project

Now you are ready to create Android project to play YouTube videos using YouTube player API. You may use IDE of your choice, here I am using Eclipse.

You need INTERNET permission to play video over internet. Add below permission in your application AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET" />

4. Define your activity layout

In this example, we will create an simple layout that uses YouTubePlayerView inside an LinearLayout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" >

        android:padding="5dp" />


5. Initialize YouTubePlayerView in your Activity

Initializing YouTube player view by calling initialize methood. Once the initialize is successful, you will get the onInitializationSuccess() callback with YouTubePlayer instance. You can add additional listeners to handle the playback control events.

package com.javatechig.youtubeapp;

import android.os.Bundle;
import android.widget.Toast;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.ErrorReason;
import com.google.android.youtube.player.YouTubePlayer.PlaybackEventListener;
import com.google.android.youtube.player.YouTubePlayer.PlayerStateChangeListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
import com.javatechig.youtubeandroid.R;

public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
	public static final String API_KEY = "YOUR API KEY";

	public static final String VIDEO_ID = "dKLftgvYsVU";

	protected void onCreate(Bundle savedInstanceState) {
		/** attaching layout xml **/

		/** Initializing YouTube player view **/
		YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
		youTubePlayerView.initialize(API_KEY, this);

	public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) {
		Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();

	public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
		/** add listeners to YouTubePlayer instance **/

		/** Start buffering **/
		if (!wasRestored) {

	private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {

		public void onBuffering(boolean arg0) {

		public void onPaused() {

		public void onPlaying() {

		public void onSeekTo(int arg0) {

		public void onStopped() {


	private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {

		public void onAdStarted() {

		public void onError(ErrorReason arg0) {

		public void onLoaded(String arg0) {

		public void onLoading() {

		public void onVideoEnded() {

		public void onVideoStarted() {

6. Output
YouTube Android Player API Example - Javatechig


A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.


Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.