How to implement Youtube Player Fragment in Android App - Androhub

Youtube Player Fragment Banner

How to implement Youtube Player Fragment in Android App

In previous article we learnt how to play a youtube video in android using YoutubePlayerView and using YoutubeBaseActivity.

But there is one problem in YoutubePlayerView that we need to extend our activity with YoutubeBaseActivity. What if we don’t want to extend our activity with YoutubeBaseActivity or we want to implement the YoutubePlayerView in fragment.

There is a solution for the above problem i.e YoutubePlayerFragment. A fragment that contains a YouTubePlayerView. Using this fragment is the preferred way of playing YouTube videos because your activity does not need to extend an activity provided by the library, as is the case with using the YouTubePlayerView directly.

So today we are going to work on the same only and we will see how to implement the YoutubePlayerFragment and play our video.

Before starting this article please go through to the previous article about YoutubePlayer and do the below mentioned tasks:

1.  Create the project in google console and get the Developer key.

2. Add then download the YoutubePlayerAPI jar file and add into your project.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. Open res ⇒ values ⇒ strings.xml and add below string values. These are some strings that we are going to use in our project.

3. So before getting started you need to download the jar file of Youtube Android Player API. Download the jar file and add into your project dependency. After adding jar to dependency  the gradle will look like below:

4. Now open your activity_main.xml and add the below code to it. In this class add the com.google.android.youtube.player.YouTubePlayerSupportFragment as fragment and one RecyclerView to display the list of videos in horizontal direction.

5. Now open your MainActivity.java and add the below code to it. In this class we need to find the id of YoutubePlayerFragment and initialise the YoutubePlayer.

6. For RecyclerView item row create new xml layout naming youtube_video_custom_layout.xml and add the YoutubeThumbnailView to it inside CardView.

7. Now create adapter for RecyclerView naming YoutubeVideoAdapter.java and add the below code to it.

Youtube Player Fragment
Youtube Player Fragment

8. Don’t forget to give Internet permission in your AndroidManifest.xml file else your app will not run.

9. Finally all done, now you can also play video in your app without using YoutubeBaseActivity.

Thanks. :)

 

14 Comments

Archana
Sunday, February 25th, 2018

I added my own api key and try to run this code but getting error like “An error occurred while initializing the youtube player”

Dr. Droid
Wednesday, February 28th, 2018

Hi Archana

Can you tell me does your project at Google console has YouTube Data API enbaled?

Does your manifest has INTERNET permission?

Thanks

Mityo
Thursday, December 27th, 2018

It works with No problems!!!

Emil
Friday, December 28th, 2018

I have been struggling to use this app on a android tv box, but I am unable to click or play any of the videos with the remote. It only works on a phone. I have tried many ways but it doesn’t seem to work. Please help.

Dr. Droid
Friday, December 28th, 2018

Hi Emil,

I don’t have any idea about TV Box as i haven’t tried it.

Actually the app is only for mobile so i think it wont work on TV.

Thanks.

Slava
Friday, March 22nd, 2019

Hi, Dr.Droid!
I have not understand, where is realisation of YoutubeViewHolder in adapter, and why I have not can use RecyclerView OnClickListener in Main class

Slava
Friday, March 22nd, 2019

Hi!
I have not understand, how and where you has realisation of YoutubeViewHolder in adapter, and why I have not can use RecyclerView OnClickListener in Main class

Dr. Droid
Saturday, March 23rd, 2019

Hi Slava,

Download the source code to get all the classes. I have pasted only classes which are important.

Thanks

Priyanka
Thursday, May 28th, 2020

Hi, Dr.Droid!

How can we play youtube videos in RecyclerView only?

summyia
Saturday, September 12th, 2020

how can we implement JSON fetched list of youtube videos in this? I am working on it but its giving errors

summyia
Saturday, September 12th, 2020

this code worked perfect for manually adding video list. But how can we implement JSON fetched list of youtube videos in this? I am working on it but its giving errors

Dr. Droid
Wednesday, September 16th, 2020

Hi Summyia,

Can you share the error which you are getting?

Thanks

SoCu
Saturday, August 20th, 2022

Hi, Dr.Droid!
It seems that it is no longer usable with androidx, it shows me the following error: “Inconvertible types; cannot cast ‘androidx.fragment.app.Fragment’ to ‘com.google.android.youtube.player.YouTubePlayerSupportFragment” hay alguna solucion

Dr. Droid
Wednesday, August 24th, 2022

Hi SoCu,

You need to migrate the project to androidX to make it work. As this project is very old.

Thanks

Post comment

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