YouTube Player Integration in Android
YouTube allows you to play it’s video in your app using the youtube player. It also provides data APIs to put like, dislike, or rate comments on a video.
To integrate youtube player you need API key from google console and you can get it using following steps:
1. Get SHA1 fingerprint first by executing “keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android” command on terminal.
2. Now visit the Developer console and create credential -> API key under the credentials section.
3. Get Android API key by passing your package name and SHA1 fingerprint that you got in step 1.
Now download the zip file available at this link Download Youtube Player.
Extract youtubeAndroidplayer.jar from the zip folder and put that in the libs folder of your project.
Open your activity class (let’s say YoutubeVideoPlayerActivity.java), the activity should extend from YouTubeBaseActivity, which will be present in YouTubeAndroidPlayerApi.jar. This activity also contains few initialization listener methods to know the status of the youtube player.
[sourcecode language=”java”]
public class YouTubePlayerActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
private static final int RECOVERY_REQUEST = 1;
private YouTubePlayerView youTubeView;
VideoView videoView;
private String videoId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoId = "fhWaJi1Hsfo";
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(Config.YOUTUBE_API_KEY, this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(Config.YOUTUBE_API_KEY, this);
}
}
private YouTubePlayer.Provider getYouTubePlayerProvider() {
return youTubeView;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
if (!wasRestored) {
youTubePlayer.cueVideo(videoId); // Plays https://www.youtube.com/watch?v=fhWaJi1Hsfo
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_REQUEST).show();
} else {
String error = String.format(getString(R.string.player_error), errorReason.toString());
Toast.makeText(this, error, Toast.LENGTH_LONG).show();
}
}
}
[/sourcecode]
In the above activity, you need to replace YOUTUBE_API_KEY with your API key you got from your console and videoId with the id of the video you want to play in your player.
In your XML file put YouTubePlayerView to play video:
[sourcecode language=”java”]
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>;
[/sourcecode]
Also, you will need internet permission in your manifest file
[sourcecode language=”java”]
uses-permission android:name="android.permission.INTERNET";
[/sourcecode]
After running the app, your youtube player will look like:
To like and dislike a video you need to call the following ‘GET’ youtube APIs.
To like a video-
To dislike a video-
Further, to get video statistics such as total number of likes, dislikes, or view count: