YouTube android API allows you to enable incorporate video playback functionality in your android application. You can load and play YouTube videos or playlist in your own android application with lots of customization.

Using YouTube API, you can control all the things like play, pause and many more. There are two types of API support credentials available; one for normal purpose and another with advanced features. If you just want to load and play you tube video in your android application with some customization use API Keys Credentials and if you want to make android app with login, uploading video from your own app using youtube api make your application using OAuth 2.0 Credentials API supports. We will also talk about OAuth 2.0 and API keys later while registering your app in Google developer console.

Previously we have already posted some tutorials like embedding and playing YouTube videos in android using WebView widget and embed and play music in android app using web view from sound cloud. In this getting started with android api tutorial, you will learn to load, play youtube videos in your android app with customization using Google YouTube Android API.

How to Embed YouTube Video in Android WebView
Embed and Play MP3 Songs/Music in Android Application Using WebView
Android Sliding Menu with WebView

Android Example: How to Integrate YouTube API in Android App (Getting Started Tutorial)

To start working with Android YouTube API you need to register your app in google developer console and they will give a key, this will be used in your application. Following is the step by step guide tutorial to implement/integrate youtube api in android application to load and play youtube video in your app.

Android Example: How to Integrate YouTube API in Android App (Getting Started Tutorial)

1. Create a new Android Project

Open your android studio or any other development IDE i.e. eclipse and create a new android project with the following information.

Application name: YouTube Android API Integration Tutorial
Company Domain: viralandroid.com
Package name: com.viralandroid.youtubeandroidapiintegrationtutorial
Minimum SDK: API 16 - Android 4.1 Jelly Bean (You can choose API 9 Android - 2.3 Gingerbread)

2. Registering Your Application in Google Developer Console

To register your app go to Google Developer Console page and follow following simple steps:

Note: Below steps may not be exactly same because google changing sites design frequently.

  • Signup to google developer account using google mail i.e. gmail if have not yet.
  • Click on create a project from dropdown icon arrow (YouTube API) at top of the page and type the project name like My YouTube Video App then click on Create button.
  • Go to API Manager from side menu and choose Registering your application under YouTube APIs and then click Enable API, then you will see a notice which saying Go to Credentials. Then, click that button.
  • The new page will appear like add credentials to your project and choose YouTube Data API v3 from which API are you using and choose Android as where will you be calling the API from. Finally choose Public data from dada accessing part and then click the button called What credentials do I need?.
  • Here you need to provide fingerprint and package name of your app. Type key name like YouTube Android API Key and then click the button Add package name and fingerprint.
  • Go to Android Studio or any other development IDE. Click Build and Generate Signed APK from android studio top menu section.
  • New window will appear there, click on Create new key, again new window will appear. Here you need to choose key store path and make a password. To choose key path click from left of the key store path and give the file name like key (it is better to save your project directory) and click OK button.
  • Provide password in the box and confirm your password re-typing in next box. Write your name in Alias and again type then confirm password that you typed earlier. In the First and Last Name section, type your name, you can type only first name and then click OK button and click Next button serially and finally click Finish button.
  • After that click on Terminal tab from the bottom of android studio. Here you need to add a command: keytool -list -v -keystore key.jks just paste it there and hit enter. Replace key with your signed saved file name if you have given other. There you’ll be asked to enter keystore password, type your password that you’d created earlier and hit enter.
  • Scroll up and find SHA1 fingerprint which will look like 1A:E2:06:12:56:A0:A9:92:96:13:CF:A9:11:69:05, copy this finger print SHA1 value and go to Google Developer Console, paste it in the box SHA-1 certificate fingerprint. Also you need to give your app package name; paste in package name box my app package name is com.viralandroid.youtubeandroidapiintegrationtutorial then click Create button.
  • In the popup window your app API key appears. You can copy and paste it to notepad and click OK button or leave it we will also get it later.

Finally, you have registered your application in Google developer console.

3. Downloading YouTube Player API and Adding in Your Project

Before starting coding, you need to download the YouTube Android Player API and import the .jar file into your application project lib folder then link that .jar file to your app build.gradle file.

  • After YouTube Player API download gets completed, extract the .zip folder and copy YouTubeAndroidPlayerApi file from libs folder.
  • Again go to your android studio switch to the project view and paste YouTubeAndroidPlayerApi.jar in app/libs folder.
  • Go to file > Project Structure or press Ctrl+Alt+Shift+S key and then click on app under modules, click on Dependencies tab, click add plus button from top right side and select file dependency, select downloaded library YouTubeAndroidPlayerApi.jar and hit OK button.
  • We are going to play YouTube videos in our android app, so we need to add internet access permission in AndroidManifest.xml file. Just add <uses-permission android:name="android.permission.INTERNET" /> in your app AndroidManifest.xml file.


4. YouTube Video Player App XML Layout File

Open your app XML layout file and add a button with an id and a YouTubePlayerView in your XML layout file. Button is to play video when you/user click on the button and YouTubePlayerView is used to display video in your android application. Following is the complete content of XML layout file.


5. YouTube Player Android API Java Activity File

In your java activity file, you need to extend your activity to YouTubeBaseActivity and declare your views like Button, YouTubePlayerView and YouTubePlayer. Then reference button and you tube player view by findViewById and initialize the YouTube listener. You need to initialize your app id that you find on Google Developer Console under app that we made above in button on click listener. Following is the complete code of java activity file.


Note: Replace AIzaSyA_bMEqSJbYilFoPeVwrxtGhKfGWfs22_g with your youtube app id and UxP3OT3lphc with YouTube video id.

6. AndroidManifest.xml File

Open your app AndroidManifest.xml File and add user internet permission. Android manifest file will look like below.


You have done all things, this is time to run your application. Run and open your YouTube Android API Integration: Getting Started with YouTube Android API app and click on Play YouTube Video button, the youtube video is played just above the button like the above demo video.

7. Download Complete Project Source Code

If you want to download complete example (YouTube Android API Integration: Getting Started with YouTube Android API) project with source code Click Here and download from GitHub.

Post a Comment

Powered by Blogger.