Breaking News

Exploring Youtube Data Api V3

Exploring Youtube Data Api V3 Tutorial





In this blog, we use YouTube Data Api V3 for Android. We will enable Youtube api by creating project on google Cloud Console and then download Youtube Api jar file and add it to our project. Then we load a video in our Youtube player View.

For Youtube Data Api V3 Listeners Tutorial click here

For Youtube Data Api V3 detailed methods click here

For creating seek to millis(jump to specific location of video) click here

To implement youtube Data Api in android you have to create a project in Goole Cloud Console and enable Youtube Data Api

create project on google developer console
create project on google developer console

  • Open Api Explorer for your Project

  • Open Api Explorer in Google Developer Console
    Open Api Explorer in Google Developer Console

  • Then Enable Youtube Data Api from Api Explorer

Enable Youtube Data Api from Google Developer Console
Enable Youtube Data Api from Google Developer Console

  • Then click on Create Credentials
Create Credentials on google Developer Console
Create Credentials on google Developer Console


  • Then create a Youtube Data Api Key
Setup Credentials on google Developers console
Setup Credentials on google Developers console


  • Then Copy that Youtube Data Api Key
Copy Youtube Data Api Key
Copy Youtube Data Api Key


  • Then Create a project in Android Studio and open Strings.xml and paste api key there.
 <string name="youtube_api_key">AIzaSy***************************13UdI</string>  

  • Then Open this link and download Youtube Data Api V3
  • Then Extract zip file and copy youtube data api.jar file and paste in your project as shown in picture below.
youtube data api in project libs folder
youtube data api in project libs folder

  • Then add implementation library in app level build.gradle file.
 implementation files('libs/YouTubeAndroidPlayerApi.jar')  

Then add Youtube player View in XML file.

XML activity
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical"  
   android:padding="10dp"  
   tools:context=".MainActivity">  
   
   <com.google.android.youtube.player.YouTubePlayerView  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:id="@+id/youtube_player"/>  
   
 </LinearLayout>  

Then implements Youtube Base Activity and implements oninitializationlistener in JAVA Activity

activity.java
 package studio.harpreet.sampleyoutubedataapi;  
   
 import androidx.appcompat.app.AppCompatActivity;  
   
 import android.content.Intent;  
 import android.os.Bundle;  
 import android.util.Log;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.EditText;  
 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.YouTubePlayerView;  
   
 import java.util.ArrayList;  
   
 import static android.content.ContentValues.TAG;  
   
 public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {  
   
   YouTubePlayerView youTubePlayerView;  
   
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
   
     youTubePlayerView = findViewById(R.id.youtube_player);  
     youTubePlayerView.initialize(getString(R.string.youtube_api_key),this);  
   
   }  
   
   @Override  
   public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {  
 
     if(!b)  
     {  
       //youTubePlayer.cueVideo("-_pacR6syDo"); //https://www.youtube.com/watch?v=-_pacR6syDo  
     }   
   
   }  
   
   @Override  
   public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {  
     if (youTubeInitializationResult.isUserRecoverableError()) {  
       youTubeInitializationResult.getErrorDialog(this, 1).show();  
     } else {  
       String error = String.format("Error initializing YouTube Player ", youTubeInitializationResult.toString());  
       Toast.makeText(this, error, Toast.LENGTH_LONG).show();  
     }  
   }  
   
   @Override  
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
     if(requestCode == 1)  
     {  
       getYoutubePlayerProvider().initialize(getString(R.string.youtube_api_key),this);  
     }  
   }  
   
   private YouTubePlayer.Provider getYoutubePlayerProvider() {  
   
     return youTubePlayerView;  
   
   }  
   
 }  

Now Run your project and it successfully runs your Youtube player and loads your video.


Follow us for more posts like this, 
Subscribe to Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

No comments