Play Youtube Video in Android Application

    The YouTube Android Player API enables you to incorporate video playback functionality into your Android applications. The API defines methods for loading and playing YouTube videos (and playlists) and for customizing and controlling the video playback experience.
    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

    Before start, you must obtain a SHA1 key based on java keytool. This is quite easy with Command Prompt (CMD) or Terminal depend on your operating system.
    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
   We will have this result:

2. Obtaining Android API KEY

    Step 1: Go to Google API Console and login with your Google Account.
    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,...):

Okey, we have finished obtaining a Project Key to use Youtube API from Google. From now on, creating a new project to "test" it.
    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

- Download the latest of version of YouTube Android Player API and extract it. Once extracted, you can find YouTubeAndroidPlayerApi.jar file inside libs folder.
- 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 this project, user will put VIDEO ID to an EditText and after click "Go" Button, app will initialize video and play.
    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

    Before finished developing, adding INTERNET permission in your AndroidManifest.xml:

<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:
pic name pic name
Switch to Full Screen mode:

(Sorry for having ads)

Share


Previous post
« Prev Post
Next post
Next Post »