Android Youtube Data API Play Youtube Video Tutorial

Youtube is now the best video sharing company and there have many active users over the world. Google provide Youtube Data API to let the developer allow to play Youtube video in their mobile application or websites. It is open source and free to use for every developer, its useful for the developer. It just need to know how to use the library to communicate with the API, nothing else to worry about how Youtube works. In this tutorial, I will let you know android Youtube data API play Youtube video in your apps.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “YoutubePlayerExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Google Developer Console

Go to https://console.developers.google.com/ and create a new project for your current application.

Enable Youtube Data API and Create API Key

After that, in the library section and click the youtube data API to enable it, then go to Credentials create an API key.

You will get an API Key. eg

AIzaSyDi97kbUT9Zek8MD495XXvz9oHrUwwPovE

Restrict API Key (Optional)

This is security purpose to let your API key access to specific application. So the hacker will not able to use your API key easily.

  • Create KeyStore, (Toolbar > Build > Generate Signted APK > Create New Button)

  • By enter “keytool -list -v -keystore (keystore location)” to Get SHA-1 signing-certificate fingerprint to restrict usage to your Android apps.

  • Submit your project package name and SHA-1 fingerprint in the Google Developer Console.

Download Youtube Android Player API library

Go to this link to install the zip file for the library https://developers.google.com/youtube/android/player/downloads/. Open the zip file and get the library from lib folder. The name of library is “YoutubeAndroidApi.jar”.

Import the jar library to your project

In the Project section, Copy and paste the jar to your project “libs” folder. You can see the example image in the below.

Add dependency

Add the jar dependency in your project so you can use the library.

Add internet permissions

Add internet permissions in your androidmanifest file so you and load the youtube video.

Create two Activities

Right click your project and create two activities which name “YoutubeActivity” and “YoutubeFragment”.

Edit activity_youtube.xml layout

Go to this file and edit the layout to add the youtube player view.

Edit activity_youtube_fragment.xml layout

Modify this file and add the framelayout so you can add fragment in the class.

Edit YoutubeActivity.java class

Go to this class and extends YoutubeBaseActivity and implement YoutubePlayer.OnInitializedListener.

Edit YoutubeFragment.java class

Modify this java class and add fragment in this class. Follow the source code below.

Edit activity_main.xml layout

Edit MainActivity.java class

Modify this class, add your API key from the Google Console. And also add the video id from the youtube like https://www.youtube.com/watch?v=(xxxxxx). The xxx is video id in the youtube website.

Run Your Project

Yeah, you are completed to use the youtube video in your android project. You can test it now.

(Android Youtube Data API Play Youtube Video)

Source Code

(Visited 353 times, 1 visits today)
Advertisements

Kristofer Ng

A crazy android and web developer. Passion on developing android application and exploring new technology as a hobby.

You may also like...

2 Responses

  1. bhavin h makwana says:

    how to get “VIDEO_ID” from json bcz i dont have static url …every time i have to update dynamic url….so plz let me know how to do dynamic url ….other wise all ur project work well…

  2. Kristofer Ng says:

    Its simple…. you can get VIDEO_ID from any youtube video.
    Example : https://www.youtube.com/watch?v=pPkf7hylUuA
    pPkf7hylUuA is the video ID 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

GET YOUR EBOOK FOR FREE

Unlock free ebook "Beginning Programming ALL-IN-ONE Desk Reference for DUMMIES" now!

We will send a copy to your email