How to make your own youtube player in android

Updated 22 September 2023

Save

If we want to show the video to our android application then we do not store the video inside the project, this will increase the app size. So instead, we can upload the video to YouTube and stream it in the app to decreases the app size.

So In this blog,

I have shown you how to play the video in your own android app.

We have been already interacting with Google APIs, So we need to get the Google Developer API Key first.

Follow below steps to obtain your Google Developer Android API Key.

How to get Google Developer Android API Key

  1. Go to Google Developer Console and select or create a new project.
  2. On the left sidebar, select APIs under APIs & auth and turn the status ON for YouTube Data API v3.
  3. On the left sidebar, select Credentials and Create new key under Public API access.
  4. When popup comes asking you to choose a platform, select Android Key.
  5. Click on create. Now you should see the API KEY on the dashboard.

Get the YouTubeAndroidPlayerApi.jar

  1. First, you have to Download the latest of a version of YouTube Android Player API.
  2. After download, extract it. Once extracted, you can find YouTubeAndroidPlayerApi.jar file inside libs folder.
  3. Paste the YouTubeAndroidPlayerApi.jar file in your project’s libs folder.

Code Implementation

Open the layout file of your activity (activity_main.xml) and add below code. This creates a simple layout with YouTubePlayerView.

For YouTubePlayerView

activity_main.xml

A view for displaying YouTube videos. Using this View directly is an alternative to using theYouTubePlayerFragment. If you choose to use this view directly, your activity needs to extend YouTubeBaseActivity.

To get started, place this view in your view hierarchy and call initialize(String, OnInitializedListener) to create a YouTubePlayer which you can use to load videos into this View.

This view will save and restore the state of the YouTubePlayer associated with the view as part of the onSaveInstanceState/onRestoreInstanceState flow.

This activity also contains few initialization listener methods to know the status of the youtube player.

MainActivity.java

Maybe this tutorial is worked for you.

Source:

https://developers.google.com/youtube/

author
. . .

Leave a Comment

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


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home