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 

9 comments:

  1. it's truly pleasant and meanful. it's truly cool blog. Connecting is exceptionally valuable thing.you have truly helped heaps of individuals who visit blog and give them usefull btclod.com

    ReplyDelete
  2. What an informative post you share here! I learn more knowledges about Youtube Data Api V3 thanks to this blog! Keep up your work! apk.care

    ReplyDelete
  3. this is awesome, i really need this information
    komicast
    noveltoon

    ReplyDelete
  4. YouTube has an incentive for upgrading address conversations of different public talking subjects and issues. 74% of the understudies overviewed showed that they like to watch a video during a show. Public talking teachers battle to discover opportune models and outlines. I as of late used a discourse found on YouTube that was conveyed to Columbia University understudies by Lee Bollinger, the leader of the college. https://www.buyyoutubeviewsindia.in/youtube-marketing/

    ReplyDelete
  5. The tip is so helpful. I will bookmark your site to read more from you. Thank for your effort. APK Download

    ReplyDelete
  6. I would like to thank you for the efforts you have made in writing this article Apkdownload

    ReplyDelete
  7. I visit your blog regularly and recommend it to all of those who wanted to enhance their knowledge with ease. The style of writing is excellent and also the content is top-notch. Thanks for that shrewdness you provide the readers! https://www.buyyoutubeviewsindia.in/youtube-marketing/

    ReplyDelete
  8. Trusted site to buy youtube subscribers India. Offer high quality, safe and legit buy youtube subscribers cheap prices and order starts in 24-48 Hrs. https://www.buyyoutubesubscribers.in/

    ReplyDelete
  9. Beaver says I also have such interest, you can read my profile here: indian visa

    ReplyDelete