1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Layouts and Animation for Android Apps

Project Android Youtube Player

Start course
Overview
Difficulty
Intermediate
Duration
4h 7m
Students
1
Description

In this course, you'll start experimenting with XML code and diving deeper into layouts, namely linear and constraint layouts. We'll also look at animations and build a few fun features with them. Then, we'll take a deeper dive into the Kotlin programming language and constraint layouts, before building a fully functioning Tic-Tac-Toe game.

On top of that, we'll then build a second app which can play YouTube videos within it, and you'll learn how to work with APIs and API keys.

Intended Audience

This course is intended for beginners to Android app development or anyone who wants to master coding in Kotlin.

Prerequisites

Since this is a beginner-level course, there are no requirements, but any previous experience with coding would be beneficial.

Transcript

Hello and welcome back. In this video, we'll begin a project where we'll build a video player which will play YouTube videos within the app. And previously, we saw how we could launch a YouTube video in the YouTube app, but in this one we'll build the player in our app itself. A few notes about this project. However, at the time of this recording Android 11 and the YouTube player API, which stands for application programming interface, which will be using to build this doesn't seem compatible. So, Google hasn't updated the YouTube player API in a while, but that may change in the near future, so by the time you see this it may have been updated already. 

But as it stands right now, we won't play this app on the existing Android 11 emulator that we have, but we'll install an emulator running Android 10 so we can play the app. So, if you already have an older Android 9 or 10 emulator, you are already set and can follow along easily. If you however, only have Android 11 emulator, you can follow along and see how we install an Android 10 emulator in this video as well. One thing to note is emulators from my experience, take up between 4-8 gigs of space in your computer, so you have to make sure you have enough space in your computer for it, otherwise you may consider deleting your existing emulator and installing a new one just for this video project. Or you can use one for this project and just remove it after the project is done, it's up to you. So, let's get started. First, we'll create a new project. 

I'm gonna select empty activity, and I will call this 'My YouTube Demo'. And for this one, minimum SDK, I'm gonna select a higher one. It shouldn't make a difference, but let's just do a higher one because we haven't done it before. Let's choose Android 51 Lollipop. All right. Finish. All right, my project is ready to go. Let's first set up Android 10 emulator. So, you see this one, the one that I have is running Android 11. And you can see here you can access AVD manager from here. You can also go over here. You see this tab right here, it says AVD manager if you pull that up. There's my emulator, so to create a new emulator, I'll just click on this, create virtual device and you can see my existing one is actually using nine gigs. 

So, it's pretty memory intensive. All right, so phone that's fine. I'll select 'Pixel 3a' slightly older than Pixel 4, but it does have the play store, so that's good. Next. And here you see the different versions of android Target Android 11, Android 10, 9 and so on. So, I'm gonna use Android 10 and I don't have this installed, so I have to download this. So, I'm gonna click on "download", this is gonna take a minute So, I'm gonna come back once the download is complete. All right. The download, completed. I'm gonna click on "finish", and you see how I have Android 10 right there. I'm gonna click "next" and the AVD name. That's fine. Portrait mode. All of this is fine. If you want to see advanced settings, you can do that down here. I'm not going to change anything. All this is good for me. I'm gonna click on finish and there you go. There is my device and if you see here size on disk 513 megs, this is going to change once I start using it. 

All right. So don't be fooled by this. They do take up a lot of space. But again, you can remove and manage your devices here depending on the project you're working on, so it's not really a big deal. So, now that I have this device, I can play it from here. But I'm not gonna do that when I close out of this. And you see over here I have the option in my available devices and this one I know is Android 11 I know is Android 11, and thiis one is Android 10. Great. So if I look at my layout file and I'm gonna click on project, there's a "hello world". So, let's play this real quick and make sure all our setup worked as we expect and I'm gonna play this in my new device running Android 10. All right. So this is taking some time as you know, this is the first time we're starting this up. It's saying system UI not responding. As usual, we're gonna click on "wait", okay?

And there it is. That took a while. Again, the first time usually takes a long time the phone comes online and all that stuff, but here we go, "Hello world". So, now that the emulator is set up, we have to take care of two set up items associated with APIs and we're gonna need the Android YouTube Player API which will download and add to our app. And again, if you have not worked with APIs before. they stand for application programming interface. And what we're gonna be doing is downloading essentially a package of files which is gonna give us the tools to have the YouTube player show up within our app and we're gonna get this from Google, and that's the part that's not working on Android 11 yet as of this recording. All right. And second will need an API key from google which will add as the second step, which is why at the end of the last video I had recommended getting a Google account if you don't already have one. 

Okay, so first the player, I'm gonna pull up a browser tab and you can google Android YouTube Player, API. And it should pop up right here from google developers. YouTube Android Player, API. If you click on this, gives you some information about the player and what it does and if you scroll down to getting started, there's the download page. So click on that and here is the file that we're gonna be downloading. It's a zip file and here's the download link. So I'll click on it. There you go, it's done. And now I'm going to pull it up. There it is. So the zip file, I'm gonna unzip it. I can just do that on Mac by double clicking on it, and Windows you may have to extract. 

I'm not really sure exactly what the commands are, but simply unzip this or extract it. So, there we go. If I double click on it, there's the folder that pops up and it should do that for you as well. Now go into this folder and go to the Libs folder within it and there is the file that we're going to need in our application, all right? It's a '.jar' file. So, right click and then copy right here. All right. And then I'll go to Android Studio within my project. I'm gonna go to my project tab right here and we're gonna paste it in our libs folder within the app . For that, you're gonna need to go to a different view right here, on top of my file tree. You see, right now I have the android view. You want go to the project view and then within the YouTube demo project that we have within here. You see, we have the app folder and there is my libs folder. You wanna paste it within this folder, so you can right click and paste, Or I'm simply gonna command V to paste it. It's gonna verify that I want to do this, Okay. That's the name of the file. Refactor. Perfect. Now, if I look at that there it is. There is the file. All right. 

Now we have to make sure that gradle actually sees or uses this file when it's building my application. And to do that, I'm gonna go to this file, okay. Have this highlighted, right click, scroll all the way down, and you see this option add as library. And I'll show you exactly what this does. So, I'm gonna click on that and you see it pops up at to module, 'myYoutube_Demo.app'. That's correct. So I'll say okay. And you see it's 'sync in' the Gradle project and again, I'll show you exactly what this does once the sync is complete. Okay. It's synced. So that library is now available to us. And again, let me show you. I'm gonna go back to my Android view, and under Gradle scripts right here. You see in our 'build.gradle' file  which we've seen before many times, scroll down. You see this line here, inplementation files, 'libs YouTubeAndroid Player.jar' That's what was added and synced. Okay. I'll close out of this. And what I want to do now is give Internet permission to my app. 

We did this before in our last mini project when we were dealing with links. So, I'm going to go to manifests and 'AndroidManifest', right here, and I'm gonna say uses permission and again Internet, okay? And then close it. All right. So once that's done, I'm gonna close out of android manifest. That's all set up. Now, the next step is to get the API key and for this we'll need to go to our Google developer console. So, I'm gonna pull up a new tab. All right. Here we are. And I'm gonna look for google developer console. All right. And you see here 'console.developers.google.com.' If I click on it, since I'm already logged in  to my google account, it's gonna take me to my developer console. But if you don't have a Google account or if you're not signed in, it'll ask you to sign in at this time and here on top you can create a new project if you don't already have a project. All right. 

So, here I have a couple of projects, I'm going to create a new project and I'll call this 'My YouTube Demo', you can call this whatever you like and to organization. That's fine. So, that's how you create a new project and then you should see it over here. My YouTube demo if I click on it. There you go. Here is the project and you see here within it. You don't have any API is available to use or go to the API library. You see it's referring to this right here in the library. So, what you wanna do is click on 'library'. Welcome to the API library. You wanna scroll down. There is YouTube and we need this. So YouTube data API V3. I'm gonna click on this and you want to enable this for your project. If you already have this enabled, then you can go to manage the API. Right now, I'm gonna enable this for this project. It's been enabled. Now, I can create the API key and I can do that in this link, create credentials. So I'm gonna go here, so add credentials to your project. 

Which API are you using? We're gonna say this one, YouTube Data API V3. And where will you be calling this API from? We're gonna select Android over here because we'll be calling it from our Android application. And what data will you be accessing? I'm gonna select public and then over here what credentials do I need? Let's click on this. There you go and get your credentials here is your API key. So that's the API key that was created for me. Click on "Done" at this time. Now, I'm gonna restrict to this. I'm gonna go to API key over here. There's the API key name that's fine. There's the key itself on the top right over here. Application restrictions, I'm gonna select Android apps and restrict usage to your Android apps. I'm gonna have to add the app that I have. 

Basically, I'm registering my app for this project, so add an item. We need the package name and you can get this right there. There is my package name. Command C, 'com.example.myYouTubedemo', so yours will be slightly different. Enter that here, and then the 'SHA-1 certificate fingerprint' of the app. So, let's go ahead and get this from Android studio. I'm gonna close this on the left. See on the right here. You see gradle. Okay, click on that and then go to app, and then tasks, Android and you see the signing report, double click on this. And this will pull up the 'SHA1 certificate' for you. Okay? Hit finished And there is the SHA1 certificate fingerprint. I'm gonna copy everything from the first digit all the way to the end of this. Okay. Command C, go back here, paste this in here. Done. 

And in terms of API restrictions below this when working in production, you may want to restrict this key but we're fine for now. So, I'm simply gonna click on "Save". All right, now this ap key is available for me to use in my app. And to use it , you can simply copy it from here. There you go. And we can paste it in our app where we need. Obviously there are different security schemes through which you can use it. We're gonna use it in a very easy way, okay? So now that our API key is ready and we have the Android YouTube player API ready to go in our app, our preliminary setup for the app is ready. So, we're gonna leave it at that in this video. In the next video, we'll set up the layout view for our app and complete our mini YouTube player app. See you there.

 

About the Author

Mashrur is a full-time programming instructor specializing in programming fundamentals, web application development, machine learning and cyber security. He has been a technology professional for over a decade and has degrees in Computer Science and Economics. His niche is building comprehensive career focused technology courses for students entering new, complex, and challenging fields in today's technology space.

Covered Topics