Easy Tutorial step by step with free source code: play videos from YouTube playlist using YouTube Android Player API in Android Studio version 1.5.1.
Learn how to download and import YouTube Android Player API and how to Register your app using YouTube Android Player API and obtain API Key.
Create new project with Application name: Play YouTube Playlist; Minimum SDK: API 10 – Android 2.3.3 (Gingerbread).
Download YouTube Android Player API: https://developers.google.com/youtube/android/player/downloads/
Copy YouTubeAndroidPlayerApi.jar file to /libs folder in your project.
Add YouTubeAndroidPlayerApi.jar library in your project (see video tutorial from below).
In manifest->AndroidManifest.xml add the permission:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.toyo.playyoutubeplaylist">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

In app->res->layout->activity_main.xml add the code:

<?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="com.app.toyo.playyoutubeplaylist.MainActivity">

    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/youtube_player"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"/>
</RelativeLayout>

In app->java->MainActivity.java complete the code:

package com.app.toyo.playyoutubeplaylist;
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;

public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

    public static final String API_KEY = "YOUR API KEY!!";
    public static final String PlayList_ID = "YOUR PLAYLIST ID!!";

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

        /** Initializing YouTube Player View **/
        YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
        youTubePlayerView.initialize(API_KEY, this);
    }

    @Override
    public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) {
        Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
    }
    @Override
    public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
        /** add listeners to YouTubePlayer instance **/
        player.setPlayerStateChangeListener(playerStateChangeListener);
        player.setPlaybackEventListener(playbackEventListener);
        /** Start buffering **/
        if (!wasRestored) {
            player.cuePlaylist(PlayList_ID);
        }
    }

    private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {
        @Override
        public void onBuffering(boolean arg0) {
        }
        @Override
        public void onPaused() {
        }
        @Override
        public void onPlaying() {
        }
        @Override
        public void onSeekTo(int arg0) {
        }
        @Override
        public void onStopped() {
        }
    };

    private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {
        @Override
        public void onAdStarted() {
        }
        @Override
        public void onError(ErrorReason arg0) {
        }
        @Override
        public void onLoaded(String arg0) {
        }
        @Override
        public void onLoading() {
        }
        @Override
        public void onVideoEnded() {
        }
        @Override
        public void onVideoStarted() {
        }
    };
}

Obtain your API Key from Google APIs: https://code.google.com/apis/console (e.g. AIzaSyCe6tORd9Ch4lx-9Ku5SQ476uS9OtZYsWA) – see video tutorial from below.

Get your Playlist ID from YouTube (e.g. PL1LAkZ-M7pNKwicHAlI3HGQMH0jofeXmd) – see video tutorial from below.

Run app in mobile device and use next and previous buttons to watch videos from youtube playlist in format portrait, landscape and full screen.
See the result of app in screenshot from mobile device:

play videos from YouTube playlist using YouTube Android Player APIThis doesn’t work on Emulator. It work only on mobile device!
Emulator can’t play YouTube videos, because YouTube have different formats (avi, mp4, mov, wmv etc) than emulator which supports only 3gp videos.

Watch the VIDEO TUTORIAL How to Play Videos PLAYLIST YouTube using YouTube Android Player API in Android Studio:

See here an video example recorded from tablet of how this app works.

App has next and prev video button, portrait and landscape orientation, quality video options and fullscreen.

Download free PlayList-YouTube.apk application from here and install it on your mobile device to see how it works.

Please subscribe here to our youtube channel. Thank you!
Check next tutorials to learn all about Android.
For further questions leave a message.

 

3 Comments

You can follow any responses to this entry through the RSS 2.0 feed.

Thanks , that help me , but how to show the playlist in android studio

thank you so much its working perfectly..
but how to display videos in list so that user can select videos from playlist. please help me

Thanks , but how to show the playlist all video and show them oncliclick particular video open in android studio

Leave a Reply

Your email address will not be published. Required fields are marked *

Blue Captcha Image Refresh

*