How to integrate Youtube API-v2 in the application?

Vikas Kumar
3 min readMar 28, 2020

--

Add Youtube Android Player Library in Android-Studio

  • Use this link to download the latest version of the YouTube Android Player API.
  • Extract downloaded RAR file and YouTubeAndroidPlayerApi from library directory to library directory in your application package then add this file as Library.
    After successful addition of the library, a dependency will automatically get added in gradle.build(module-level)
    implementation file(‘libs/YouTubeAndroidPlayerApi.jar')
  • Use this link to setup instruction for Youtube API, After successful login and creating a project on this site this screen may appear;
  • Click on ENABLE APIS AND SERVICES and search for Youtube Data API v3, enable this API.
Screen after enabling Youtube Data API v3
  • Click on CREATE CREDENTIALS and Setup Youtube Data API v3, Android and Publick data in question Which API are you using?, Where will you be calling the API from and what data will you be accessing respectively.
  • Now, edit your API key as Android apps, Package name, SHA-1 certificate fingerprint and last don't restrict key
Screen after editing the API Key

Give Internet permission in projects Manifests file <uses-permission android:name="android.permission.INTERNET"></uses-permission>

Code in Activity XML file

<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"
tools:context=".MainActivity">
<com.google.android.youtube.player.YouTubePlayerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/youtubeviews"></com.google.android.youtube.player.YouTubePlayerView>
</LinearLayout>

Code in Activity Java file

Overrides methods used in YoutubeScreenActivity are:

  1. onFullscreen YouTubePlayer.OnFullscreenListner
    Called when fullscreen mode changes,
    parameter true if the player should enter fullscreen, false otherwise
  2. onInitializationSuccess YouTubePlayer.OnInitializedListner
    Called when initialization of the player succeeds,
  3. onInitializationFailure YouTubePlayer.OnInitializedListner
    Called when initialization of the player fails
  4. onActivityResult Helps in getting the result from an activity
  5. onConfigurationChanged
    This method is passed a Configuration an object that specifies the new device configuration. By reading fields in the Configuration, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your activity's Resources an object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your activity.
  6. onKeyDown KeyEvent.Callback
    Called when a key down event has occurred
  7. onError YouTubePlayer.ErrorReason
    Called when an error occurs. This can be called from any state.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Vikas Kumar
Vikas Kumar

Written by Vikas Kumar

Android Developer | Actions on Google | GCP | Tech-Blogger | Mentor | Open Source Contributor| CA at Coding Blocks

No responses yet

Write a response