Thursday 20 April 2017

Android YouTube Integration Example


YouTube Integration





  • Create Application

First step in registering application is create a new application developer console. 
So, go to Google Developer Console. Create a new project, named YoutubeVideo.

  • Enable YouTube API

To use API, first you need to enable them in the dashboard.
So, click on the Enable and manage APIs.
This will redirect you to list of all the Google APIs. Here, click on the YouTube Data API to enable YouTube API for your project.

In the resulting screen, click on Enable button which will enable the API andwill ask you to create credentials for your application.



After enabling the API, you need to create the credentials to authenticate app. To create credentials,
click on Go To Credentials button in the dashboard.




This will redirect you to create Android Key form.





  • Download Library

First step in integrating YouTube Player API is downloading the YouTube Android Player API Library. Use YouTube Android Player API link to download the latest library and move it to app’s libs folder. Now, you need to add the library as application dependency. To do so, open app’s build.gradle file and add the below line as dependency :
androidTestCompile 'com.android.support.test:runner:0.5' androidTestCompile 'com.android.support:support-annotations:25.2.0'

  • AndroidManjfest.xml

<uses-permission android:name="android.permission.INTERNET"/>

  • Activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.youtubevideo.MainActivity"> <com.google.android.youtube.player.YouTubePlayerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/youtube_player_view"> </com.google.android.youtube.player.YouTubePlayerView> </RelativeLayout>

  • MainActivity.java

package com.youtubevideo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; 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; public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener { //AIzaSyB6dzPRjIZNe_OE6kJRes6370TV4vrTnL0 public static final String API_KEY = "AIzaSyB6dzPRjIZNe_OE6kJRes6370TV4vrTnL0"; //https://www.youtube.com/watch?v=<VIDEO_ID> public static final String VIDEO_ID = "3VxWcqo2sqs"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initializing YouTube player view YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player_view); youTubePlayerView.initialize(API_KEY, this); } @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) { if(null== player) return; // Start buffering if (!wasRestored) { player.cueVideo(VIDEO_ID); } // Add listeners to YouTubePlayer instance player.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() { @Override public void onAdStarted() { } @Override public void onError(YouTubePlayer.ErrorReason arg0) { } @Override public void onLoaded(String arg0) { } @Override public void onLoading() { } @Override public void onVideoEnded() { } @Override public void onVideoStarted() { } }); player.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() { @Override public void onBuffering(boolean arg0) { } @Override public void onPaused() { } @Override public void onPlaying() { } @Override public void onSeekTo(int arg0) { } @Override public void onStopped() { } }); } @Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { Toast.makeText(this, "Failed to initialize.", Toast.LENGTH_LONG).show(); } }








No comments:

Post a Comment