Play YouTube video in android example

This tutorial we will learn about How to play YouTube video in Android, here is the preview image looks like.

Android Youtube player

  • Register application in Google API console, input SHA-1 debug key, package name & generate the Android key. this key is required to initialise Youtube video player.
keytool -exportcert -alias androiddebugkey -keystore C:\Users\feelzdroid\.android\debug.keystore -list -v

Google_API_Console

  • Download the YouTube Android Player API, from Libs folder copy “YouTubeAndroidPlayerApi.jar” file. In Android studio go to Project–>Libs folder & past it in, then add entry “libs/YouTubeAndroidPlayerApi.jar” in build.gradle.Setup Youtube player jar file android studio

Youtube player setup gradle

  • Create a Fragment in the Layout and set. “com.google.android.youtube.player.YouTubePlayerFragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="feelzdroid.testyoutube.MainActivity">

<TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:id="@+id/id_video_play_text"
 android:text="Playing Youtube video in Android app" />


 <fragment
 android:layout_below="@+id/id_video_play_text"
 android:id="@+id/youtube_player_fragment"
 android:name="com.google.android.youtube.player.YouTubePlayerFragment"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"/>

</RelativeLayout>
  • In order to Play Youtube video in Android the activity should implements “YouTubePlayer.OnInitializedListener“.
public class MainActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener{

    private YouTubePlayerFragment playerFragment;
    private YouTubePlayer mPlayer;
    private String YouTubeKey = "AIzaSyCxeQaLuDvg1KxCqViujHJp7ZiFn4O6Vzw";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        playerFragment =
                (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_player_fragment);

        playerFragment.initialize(YouTubeKey, this);

    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
                                        boolean wasRestored) {
        mPlayer = player;

        //Enables automatic control of orientation
        mPlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION); 

        //Show full screen in landscape mode always
        mPlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE); 

        //System controls will appear automatically
        mPlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI);

        if (!wasRestored) {
            //player.cueVideo("9rLZYyMbJic");
            mPlayer.loadVideo("9rLZYyMbJic");
        }
        else
        {
            mPlayer.play();
        }
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider,
                                        YouTubeInitializationResult errorReason) {
        mPlayer = null;
    }

}
  • Video thumbnail or preview on Load use cueVideo() method.
  • Play YouTube video on start of the activity use loadVideo() function.