How to play a YouTube video under Android
In this post we are going to show you guys how to play a video in your app straight from YouTube. This tutorial is divided into the following sections.
- Downloading the YouTube API library
- Android app code
- Keystore generation
- Generating YouTube API key
Downloading the YouTube API library
At first you need to download the YouTube API library. Go to the https://developers.google.com/youtube/android/player/downloads/, click the download link and save the ZIP file. Inside this file you will find the library – YouTubeAndroidPlayerApi.jar
.
Android app code
Here is an activity’s code that opens a video player and plays the video.
VideoActivity
package com.itcuties.samples.apps; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; import com.google.android.youtube.player.YouTubeInitializationResult; import com.google.android.youtube.player.YouTubeStandalonePlayer; public class VideoActivity extends Activity { private int REQ_PLAYER_CODE = 1; private static String YT_KEY = "YOUR-YOUTUBE-KEY-HERE"; private static String VIDEO_ID = "sjOfxnlGAF4"; // Your video id here @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video); Intent videoIntent = YouTubeStandalonePlayer.createVideoIntent(this, YT_KEY, VIDEO_ID, 0, true, false); startActivityForResult(videoIntent, REQ_PLAYER_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQ_PLAYER_CODE && resultCode != RESULT_OK) { YouTubeInitializationResult errorReason = YouTubeStandalonePlayer.getReturnedInitializationResult(data); if (errorReason.isUserRecoverableError()) { errorReason.getErrorDialog(this, 0).show(); } else { String errorMessage = String.format("PLAYER ERROR!!", errorReason.toString()); Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show(); } } } }
What does this code do? In the onCreate
method an Intent is created using YouTubeStandalonePlayer.createVideoIntent
. As you see you need to enter the YouTube API and a Video ID as a method parameter to the call. Next an activity is started using startActivityForResult
. If something goes wrong with the newly started activity error messages are displayed on the screen. Error handling behavior is coded in the onActivityResult
method.
Keystore generation
You need to generate a keystore and a certificate you will be using for signing up your application APK file in order to generate a YouTube API key. The easiest way is to do it while you are exporting your application to the APK file.
1. Right click on your project and pick Android Tools
and then Export Signed Android package...
2. Pick your project that you want to export using the Browse...
button and click Next
3. Pick Create new keystore
option and enter Location
, Password
and Confirm
values. Click Next
.
4. Now you need to create a key in your newly create keystore. Enter Alias
, Password
, Confirm
, Validity
and First and Last Name
values. Click Next
.
5. At the end just pick a location where you want your APK file to be generated and click Finish
. You have generated signed APK file as well as the new keystore containing one certificate.
Generating YouTube API key
1. Log in to your Google account and go to Google APIs Console at https://code.google.com/apis/console/.
2. Pick Create...
option.
3. Enter API project name and click Create project
button.
4. Pick your newly create project from the list and and navigate to Services
section.
5. Scroll down and activate YouTube Data API v3
option.
6. Next navigate to the API Access
section and click Create new Android key...
button.
7. Now leave the browser for a moment and open your command line. Enter the following command
keytool.exe -list -v -keystore d:\Workspace\Android\itckeystore
You will find the keytool in your JDK installation folder.
Copy the
SH1 fingerprint
value from the terminal.
8. Go back to the browser and enter certificate SH1 fingerprint
value and end it with the ‘;’ sign and the package name your application is released under (AndroidManifest.xml
-> manifest tag
-> package attribute
). Click Create
button.
9. You will find your the key that you need to use in your application under the Key for Android apps section
.
Download this sample code here.
This code is available on our GitHub repository as well.
Hi,
Thanks for this good knowledge. I am an amateur developer. I do all steps. And I need put ads to this application and also I cannot put exit buton for app. I should put banner and ads sdk for create video programs for playstore. Please help about it. Thank you.
Ali
I have video in my rss app … how can I play this videos? setjavascriptenabled(true) in webview in not a good idea…
Hello Dear,
thanks for your nice toturial but on my emulator in Eclipse it shows Application stops working error.
Do you have any idea why!? Thanks a lot
I have added Android key and video ID carefully so there is no issue with variabales.
Thanks for any response!