# Youtube-API

# Activity extending YouTubeBaseActivity

public class CustomYouTubeActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener, YouTubePlayer.PlayerStateChangeListener {

    private YouTubePlayerView mPlayerView;
    private YouTubePlayer mYouTubePlayer;
    private String mVideoId = "B08iLAtS3AQ";
    private String mApiKey;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mApiKey = Config.YOUTUBE_API_KEY;
        mPlayerView = new YouTubePlayerView(this);
        mPlayerView.initialize(mApiKey, this); // setting up OnInitializedListener
        addContentView(mPlayerView, new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT)); //show it in full screen
    }

    //Called when initialization of the player succeeds.
    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                        YouTubePlayer player,
                                        boolean wasRestored) {

        player.setPlayerStateChangeListener(this); // setting up the player state change listener
        this.mYouTubePlayer = player;
        if (!wasRestored)
            player.cueVideo(mVideoId);
    }

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

        Toast.makeText(this, "Error While initializing", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onAdStarted() {
    }

    @Override
    public void onLoaded(String videoId) { //video has been loaded
        if(!TextUtils.isEmpty(mVideoId) && !this.isFinishing() && mYouTubePlayer != null)
            mYouTubePlayer.play(); // if we dont call play then video will not auto play, but user still has the option to play via play button
    }

    @Override
    public void onLoading() {
    }

    @Override
    public void onVideoEnded() {
    }

    @Override
    public void onVideoStarted() {

    }

    @Override
    public void onError(ErrorReason reason) {
        Log.e("onError", "onError : " + reason.name());
    }

}

# Launching StandAlonePlayerActivity

  • Launch standalone player activity
         Intent standAlonePlayerIntent = YouTubeStandalonePlayer.createVideoIntent((Activity) context,
                 Config.YOUTUBE_API_KEY, // which you have created in step 3
                 videoId, // video which is to be played
                 100,     //The time, in milliseconds, where playback should start in the video
                 true,    //autoplay or not
                 false);   //lightbox mode or not; false will show in fullscreen
         context.startActivity(standAlonePlayerIntent);
    
    
  • # Consuming YouTube Data API on Android

    This example will guide you how to get playlist data using the YouTube Data API on Android.

    SHA-1 fingerprint

    First you need to get an SHA-1 fingerprint for your machine. There are various methods for retrieving it. You can choose any method provided in this Q&A (opens new window).

    Google API console and YouTube key for Android

    Now that you have an SHA-1 fingerprint, open the Google API console and create a project. Go to this page (opens new window) and create a project using that SHA-1 key and enable the YouTube Data API. Now you will get a key. This key will be used to send requests from Android and fetch data.

    Gradle part

    You will have to add the following lines to your Gradle file for the YouTube Data API:

    compile 'com.google.apis:google-api-services-youtube:v3-rev183-1.22.0'
    
    

    In order to use YouTube's native client to send requests, we have to add the following lines in Gradle:

    compile 'com.google.http-client:google-http-client-android:+'
    compile 'com.google.api-client:google-api-client-android:+'
    compile 'com.google.api-client:google-api-client-gson:+'
    
    

    The following configuration also needs to be added in Gradle in order to avoid conflicts:

    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.2'
    } 
    
    

    Below it is shown how the gradle.build would finally look like.

    build.gradle

    apply plugin: 'com.android.application'
    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.2"
        defaultConfig {
            applicationId "com.aam.skillschool"
            minSdkVersion 19
            targetSdkVersion 25
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        configurations.all {
            resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.2'
        }
    }
    
    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.google.apis:google-api-services-youtube:v3-rev183-1.22.0'
        compile 'com.android.support:appcompat-v7:25.3.1'
        compile 'com.android.support:support-v4:25.3.1'
        compile 'com.google.http-client:google-http-client-android:+'
        compile 'com.google.api-client:google-api-client-android:+'
        compile 'com.google.api-client:google-api-client-gson:+'
    }
    
    

    Now comes the Java part. Since we will be using HttpTransport for networking and GsonFactory for converting JSON into POJO, we don't need any other library to send any requests.

    Now I want to show how to get playlists via the YouTube API by providing the playlist IDs. For this task I will use AsyncTask. To understand how we request parameters and to understand the flow, please take a look at the YouTube Data API (opens new window).

    public class GetPlaylistDataAsyncTask extends AsyncTask<String[], Void, PlaylistListResponse> {
        private static final String YOUTUBE_PLAYLIST_PART = "snippet";
        private static final String YOUTUBE_PLAYLIST_FIELDS = "items(id,snippet(title))";
    
        private YouTube mYouTubeDataApi;
    
        public GetPlaylistDataAsyncTask(YouTube api) {
            mYouTubeDataApi = api;
        }
    
        @Override
        protected PlaylistListResponse doInBackground(String[]... params) {
    
            final String[] playlistIds = params[0];
    
            PlaylistListResponse playlistListResponse;
            try {
                playlistListResponse = mYouTubeDataApi.playlists()
                        .list(YOUTUBE_PLAYLIST_PART)
                        .setId(TextUtils.join(",", playlistIds))
                        .setFields(YOUTUBE_PLAYLIST_FIELDS)
                        .setKey(AppConstants.YOUTUBE_KEY) //Here you will have to provide the keys
                        .execute();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
    
            return playlistListResponse;
        }
    }
    
    

    The above asynchronous task will return an instance of PlaylistListResponse which is a build-in class of the YouTube SDK. It has all the required fields, so we don't have to create POJOs ourself.

    Finally, in our MainActivity we will have to do the following:

    public class MainActivity extends AppCompatActivity {
        private YouTube mYoutubeDataApi;
        private final GsonFactory mJsonFactory = new GsonFactory();
        private final HttpTransport mTransport = AndroidHttp.newCompatibleTransport();
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_review);
            mYoutubeDataApi = new YouTube.Builder(mTransport, mJsonFactory, null)
                    .setApplicationName(getResources().getString(R.string.app_name))
                    .build();
            String[] ids = {"some playlists ids here seperated by "," };
            new GetPlaylistDataAsyncTask(mYoutubeDataApi) {
                ProgressDialog progressDialog = new ProgressDialog(getActivity());
    
                @Override
                protected void onPreExecute() {
                    progressDialog.setTitle("Please wait.....");
                    progressDialog.show();
                    super.onPreExecute();
                }
    
                @Override
                protected void onPostExecute(PlaylistListResponse playlistListResponse) {
                    super.onPostExecute(playlistListResponse);
                    //Here we get the playlist data
                    progressDialog.dismiss();
                    Log.d(TAG, playlistListResponse.toString());
                }
            }.execute(ids);
        }
    }
    
    

    # YoutubePlayerFragment in portrait Activty

    The following code implements a simple YoutubePlayerFragment. The activity's layout is locked in portrait mode and when orientation changes or the user clicks full screen at the YoutubePlayer it turns to lansscape with the YoutubePlayer filling the screen. The YoutubePlayerFragment does not need to extend an activity provided by the Youtube library. It needs to implement YouTubePlayer.OnInitializedListener in order to get the YoutubePlayer initialized. So our Activity's class is the following

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.widget.Toast;
    
    import com.google.android.youtube.player.YouTubeInitializationResult;
    import com.google.android.youtube.player.YouTubePlayer;
    import com.google.android.youtube.player.YouTubePlayerFragment;
    
    public class MainActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {
       
        public static final String API_KEY ;
        public static final String VIDEO_ID = "B08iLAtS3AQ";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            YouTubePlayerFragment youTubePlayerFragment = (YouTubePlayerFragment) getFragmentManager()
                    .findFragmentById(R.id.youtubeplayerfragment);
    
            youTubePlayerFragment.initialize(API_KEY, this);
    
        }
    
        /**
         *
         * @param provider The provider which was used to initialize the YouTubePlayer
         * @param youTubePlayer A YouTubePlayer which can be used to control video playback in the provider.
         * @param wasRestored Whether the player was restored from a previously saved state, as part of the YouTubePlayerView
         *                    or YouTubePlayerFragment restoring its state. true usually means playback is resuming from where
         *                    the user expects it would, and that a new video should not be loaded
         */
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
                     
    youTubePlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION |
                        YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
    
    
            if(!wasRestored) {
                youTubePlayer.cueVideo(VIDEO_ID);
            }
        }
    
        /**
         *
         * @param provider The provider which failed to initialize a YouTubePlayer.
         * @param error The reason for this failure, along with potential resolutions to this failure.
         */
        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult error) {
            
            final int REQUEST_CODE = 1;
    
            if(error.isUserRecoverableError()) {
                error.getErrorDialog(this,REQUEST_CODE).show();
            } else {
                String errorMessage = String.format("There was an error initializing the YoutubePlayer (%1$s)", error.toString());
                Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
            }
        }
    }
    
    

    A YoutubePlayerFragment can be added to the activity's layout xaml as followed

    <LinearLayout 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:orientation="vertical"
                  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=".MainActivity">
    
        <fragment
            android:id="@+id/youtubeplayerfragment"
            android:name="com.google.android.youtube.player.YouTubePlayerFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="20dp"
                    android:text="This is a YoutubePlayerFragment example"
                    android:textStyle="bold"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
    
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="20dp"
                    android:text="This is a YoutubePlayerFragment example"
                    android:textStyle="bold"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="20dp"
                    android:text="This is a YoutubePlayerFragment example"
                    android:textStyle="bold"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="20dp"
                    android:text="This is a YoutubePlayerFragment example"
                    android:textStyle="bold"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="20dp"
                    android:text="This is a YoutubePlayerFragment example"
                    android:textStyle="bold"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="20dp"
                    android:text="This is a YoutubePlayerFragment example"
                    android:textStyle="bold"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="20dp"
                    android:text="This is a YoutubePlayerFragment example"
                    android:textStyle="bold"/>
    
            </LinearLayout>
        </ScrollView>
    
    </LinearLayout>
    
    

    Lastly you need to add the following attributes in your Manifest file inside the activity's tag

    android:configChanges="keyboardHidden|orientation|screenSize"
    android:screenOrientation="portrait"
    
    

    # YouTube Player API

    Obtaining the Android API Key :

    First you'll need to get the SHA-1 fingerprint on your machine using java keytool. Execute the below command in cmd/terminal to get the SHA-1 fingerprint.

    keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
    
    

    MainActivity.java

    public class Activity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
    
        private static final int RECOVERY_DIALOG_REQUEST = 1;
    
        // YouTube player view
        private YouTubePlayerView youTubeView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
            setContentView(R.layout.activity_main);
    
            youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
    
            // Initializing video player with developer key
            youTubeView.initialize(Config.DEVELOPER_KEY, this);
    
        }
    
        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider,
                                            YouTubeInitializationResult errorReason) {
            if (errorReason.isUserRecoverableError()) {
                errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
            } else {
                String errorMessage = String.format(
                        getString(R.string.error_player), errorReason.toString());
                Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
            }
        }
    
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                            YouTubePlayer player, boolean wasRestored) {
            if (!wasRestored) {
    
                // loadVideo() will auto play video
                // Use cueVideo() method, if you don't want to play it automatically
                player.loadVideo(Config.YOUTUBE_VIDEO_CODE);
    
                // Hiding player controls
                player.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
            }
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == RECOVERY_DIALOG_REQUEST) {
                // Retry initialization if user performed a recovery action
                getYouTubePlayerProvider().initialize(Config.DEVELOPER_KEY, this);
            }
        }
    
        private YouTubePlayer.Provider getYouTubePlayerProvider() {
            return (YouTubePlayerView) findViewById(R.id.youtube_view);
        }    
    }
    
    

    Now create Config.java file. This file holds the Google Console API developer key and YouTube video id

    Config.java

    public class Config {
    
        // Developer key
        public static final String DEVELOPER_KEY = "AIzaSyDZtE10od_hXM5aXYEh6Zn7c6brV9ZjKuk";
    
        // YouTube video id
        public static final String YOUTUBE_VIDEO_CODE = "_oEA18Y8gM0";
    }
    
    

    xml file

    <com.google.android.youtube.player.YouTubePlayerView
                    android:id="@+id/youtube_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="30dp" />
    
    

    # Remarks

  • First of all, you need to download the latest jar from below link [https://developers.google.com/youtube/android/player/downloads/](https://developers.google.com/youtube/android/player/downloads/)
  • You need to include this jar in your project. Copy and paste this jar in libs folder and don't forget to add it in gradle file dependencies { compile files('libs/YouTubeAndroidPlayerApi.jar') }
  • 1. You need an api key to access youtube api's. Follow this link : [https://developers.google.com/youtube/android/player/register](https://developers.google.com/youtube/android/player/register) to generate your api key.
  • Clean and Build your project. Now you're ready to use the YoutubeAndroidPlayerApi For playing an youtube video, you need to have video id corresponding to it so that you can play it on youtube. For e.g : [https://www.youtube.com/watch?v=B08iLAtS3AQ](https://www.youtube.com/watch?v=B08iLAtS3AQ), B08iLAtS3AQ is video id which you need to play it on youtube.