how to embed and play youtube video in android app

Posted in Android By Raj On February 23, 2017

In this article, I am going to cover how to embed youtube video in the android app, placing video inside app not a good design when it comes to size and maintenance of the mobile app, now a days every client is preferring to host videos on external video hosting provider’s for better performance and maintainabilitiy

here, I am going to cover basic thing just playing single video in android screen, for more advanced thing refer my other article

The YouTube Android Player API enables you to load youtube video in the android applications, we need to download this and keep it into the libs folder of our android application

Before downloading youtube android player api, you need to register your mobile application in the google devlopers dashboard, for this you need to login to developers console and create a new project called “android-youtube-player-api”

On the left side panel you will find Library link, on clicking of the Library link it will list out all the Google API’s in the main panel

Check for YouTube APIs section in the main panel then click on “YouTube Data API” and click on Enable link

You need to have credentials to access youtub data api so, click on create credentials button and select “where will you be calling the API from to “Android” and what data you will be accessing to “public data” and click on what creadentials button to generate public key and finally click on done

The generated key is unrestricted one and we should not use this for production deployment, you need to generate by selceting appropriate  appropriate selections in the Key restriction section,  we need to use API key in our android project so note it down on the notepad


Creating Android Project

Open Android Studio,  create a new project and enter application name is “YouTubeVideoList” fill all mandatory fields once the project is created then open build.gradle file and add below entries

Open androidmanifest.xml file and add network permissions entry

The MainActivity class should extend from the YouTubeBaseActivity and implement OnInitializedListener, we must override two methods here, onInitializationSuccess and onInitializationFailure, if the initialization of player successful then the player will play the youtube video in the app, if not onInitializationFailure will fire and simply displays error message


Go to the layout folder and open the content_main.xml file and enter the below code for generating a layout , this layout plays youtube video in the app