Using the API, you can load or cue videos into a player view embedded in your application's UI. You can then control playback programmatically. For example, you can play, pause, or seek to a specific point in the currently loaded video.
The fact that there are a lot of apps in Google Play embedded Youtube Video Player. So we can be confident that Youtube API is widely used.
In this tutorial we are going to learn how to play YouTube video in a application. This app will have a single screen with a video playing in it. This article covers very basics of YouTube Android API.
1. Making SHA-1 Fingerprint
In Windows, go to installed Java JRE bin folder (usally is "C:\Program Files\Java\jre1.8.0_45\bin"), run CMD and type this line:
keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
In Mac or Ubuntu, your command will be like this:
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
2. Obtaining Android API KEY
Step 2: Creating a new project to use Google API by click Create Project button:
Step 3: On the left sidebar, select APIs under APIs & auth and Click Enable API button.
Step 4: Select Youtube Data API:
Click Enable button to complete this step:
Step 5: On the left sidebar, select Credentials and Create new key under Public API acess. When popup comes asking you to choose platform, select Android Key.
Step 6: Paste above SHA-1 key and your project’s package name separated by semicolon(;):
Step 7: After click Create button, you will see your new API key and some other details (project name, date and time created, author email,...):
In this tutorial we are going to learn how to play YouTube video in a application. This app will have a single screen with a video playing in it. This article covers very basics of YouTube Android API.
3. Download Youtube API and import to project
- Create a new Android Studio Project and put this jar file to "app/libs" folder (use in module app).
- Right click at jar file and select Add as a Library... IDE will auto-sync gradle for you.
For more details about import jar library to Android Studio, see my previous post.
4. Developing Android Project
In order to playing Youtube Video, you must declaring an YoutubePlayerView in your xml, your video content will be "embedded" in it. Activity layout like this:
For playing video, your Activity must extends YoutubeBaseActivity and we must provide a YouTubePlayer.OnInitializedListener method:
After click button, YoutubePlayer will be created. We describe in handle button event method (onClicListener()):
Finally, we have full MainActivity.java code:
5. Running application
<uses-permission android:name="android.permission.INTERNET"/>
In Youtube Video link, VIDEO ID is locate after "=". For example, if link is "https://www.youtube.com/watch?v=N4DqraJucpA" so this VIDEO ID is "N4DqraJucpA".
Type this ID to EditText, click Button, we can watch this video:
(Sorry for having ads)