How to integrate Youtube API-v2 in the application?

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.

- 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

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:
- onFullscreen
YouTubePlayer.OnFullscreenListner
Called when fullscreen mode changes,
parametertrue
if the player should enter fullscreen,false
otherwise - onInitializationSuccess
YouTubePlayer.OnInitializedListner
Called when initialization of the player succeeds, - onInitializationFailure
YouTubePlayer.OnInitializedListner
Called when initialization of the player fails - onActivityResult Helps in getting the result from an activity
- onConfigurationChanged
This method is passed aConfiguration
an object that specifies the new device configuration. By reading fields in theConfiguration
, 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'sResources
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. - onKeyDown
KeyEvent.Callback
Called when a key down event has occurred - onError
YouTubePlayer.ErrorReason
Called when an error occurs. This can be called from any state.
Congratulations, You have successfully integrated Youtube API-v2.

Clap 👏🏻👏🏻 50 times and share the article if you like it. Thank you for giving your valuable time :)
Resources
- https://developers.google.com/youtube/android/player/downloads/
- https://console.developers.google.com/
- https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayer.OnFullscreenListener
- https://developer.android.com/training/basics/intents/result
- https://developer.android.com/guide/topics/resources/runtime-changes
- https://developer.android.com/reference/android/view/KeyEvent.Callback
- https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayer.PlayerStateChangeListener.html#public-abstract-void-onerror-youtubeplayer.errorreason-reason