Hello Friends, Today we are going to learn how to play a youtube video in our app using YoutubePlayerAPI. And also I will also show you how to show thumbnails of the youtube video.
This app will be having two screens:
1. Now login into your Gmail Account and go to Google API Console.
2. Click on plus button to Create New Project.
3. Enter your project name and click Create.
4. Creating project will take some couple of minutes you can check the processing of your project on top right corner. After successful creation of your project the below is the first screen you get i.e Project Dashboard.
5. Now to integrate YoutubePlayer API we need to enable Youtube Data API v3 to do that click on Enable APIS and Services.
6. After clicking on Enable APIS and Services you will get below screen containing list of Services provided by Google. Now click on Youtube Data API v3 from 2nd row 1st item.
7. After clicking on Youtube Data API v3 you will get below screen. Here you need to click on Enable button to enable the API.
8. After enabling Youtube Data API v3 you will get below screen. Here you need to click on Create Credentials to get API key.
9. On clicking Create Credential you will get below screen. Here you again need to click on Create Credentials.
On clicking Create Credentials you will get a dropdown from that you need to select API key.
10. Finally after clicking on API key you will get API key to integrate Youtube Data API in your app. Copy this API key and paste in your project inside Constants.java class.
1. Now click on Restrict Key option from API key created dialog. By restricting API key you can prevent unauthorised use in production. On clicking you will get below screen. Here select Android apps radio button and below add your app package name and SHA-1 Certificate Fingerprint and click on Save button.
If you don’t know how to generate SHA-1 key you can check this link.
2. If you have closed the API key created dialog then also you can restrict the API key by clicking on Pencil icon from the below screen you get after closing dialog.
Now all done from Google API Console side come back to Android Studio.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<resources> <string name="app_name">Youtube Android Player API Demo</string> <string name="thumbnail_image_view_desc">Youtube Video thumbnail image view</string> <string name="close_player_activity">Close Player activity</string> <!-- we need to use video ids instead of video link --> <string-array name="video_id_array"> <item>CqhpNxI8qYw</item> <item>sasCrpgHDy8</item> <item>iJeh3x09Ya8</item> <item>B5zRaD7B504</item> <item>9qWHzj8k-Uo</item> <item>RjiK45wC924</item> <item>RH3zaLO8H4E</item> <item>o14pyzosjvk</item> <item>5SQrpBQne7A</item> <item>5WJ3r67ORxo</item> </string-array> <string-array name="video_title_array"> <item>Android Security Lock Demo</item> <item>Android Sectioned RecyclerView Demo</item> <item>Android Home Screen Widgets Demo</item> <item>Android Floating Widget like Facebook Messenger Chat Head Demo</item> <item>Android Google Maps Introduction Demo</item> <item>Android Lollipop Activity Transitions Demo</item> <item>Android Toolbar Animation Using Design Support Library Demo</item> <item>Android Material Advance Tabs Demo</item> <item>Android Drag and Drop Demo</item> <item>Android Fingerprint Authentication Demo</item> </string-array> <string-array name="video_duration_array"> <item>02:48</item> <item>00:44</item> <item>01:01</item> <item>00:55</item> <item>01:03</item> <item>00:25</item> <item>00:34</item> <item>00:45</item> <item>00:52</item> <item>00:28</item> </string-array> </resources> |
In the above string.xml file if you see i have created an array of Video IDs, where Video ID is the ID of youtube video. If you don’t know how to get ID from video then go to Youtube and open any video link. Now see the below image to get the Video ID from opened youtube link.
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:
1 2 3 4 5 6 7 8 9 |
dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:27.0.0' implementation 'com.android.support:cardview-v7:27.0.0' implementation 'com.android.support:recyclerview-v7:27.0.0' //Youtube Android Player API Jar dependency implementation project(':YouTubeAndroidPlayerApi') } |
NOTE : I am using Android Studio 3.0 so compile is now replaced with implementation.
4. Now open your activity_main.xml and add the RecyclerView to it for displaying the list of videos with thumbnail.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#EEEEEE" android:orientation="vertical" tools:context="com.youtubeandroidplayerapidemo.main.MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:minHeight="?attr/actionBarSize" app:title="@string/app_name" app:titleTextColor="@android:color/white" /> <!-- recycler view to show the list of youtube videos --> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="10dp" android:padding="10dp" /> </LinearLayout> |
5. For RecyclerView custom row we need to create one more layout naming youtube_video_custom_layout.xml and add the below code to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginEnd="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginStart="10dp" android:layout_marginTop="10dp" app:cardCornerRadius="4dp" app:contentPadding="10dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- youtube thumbnail view to show the thumbnail of videos --> <com.google.android.youtube.player.YouTubeThumbnailView android:id="@+id/video_thumbnail_image_view" android:layout_width="120dp" android:layout_height="120dp" android:background="@android:color/darker_gray" android:contentDescription="@string/thumbnail_image_view_desc" android:scaleType="centerCrop" /> <TextView android:id="@+id/video_title_label" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:layout_toEndOf="@+id/video_thumbnail_image_view" android:layout_toRightOf="@+id/video_thumbnail_image_view" android:text="Title of Video" android:textColor="@android:color/black" android:textSize="16sp" /> <TextView android:id="@+id/video_duration_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:drawableLeft="@drawable/ic_action_duration" android:drawablePadding="2dp" android:gravity="center_vertical|end" android:text="24.33min" android:textColor="@android:color/primary_text_light" android:textSize="14sp" /> </RelativeLayout> </android.support.v7.widget.CardView> |
In the above layout we are using com.google.android.youtube.player.YouTubeThumbnailView which is a part of YoutubePlayerAPI jar file that we had downloaded and added to our project. This YouTubeThumbnailView is responsible to show the thumbnail of the videos.
6. After creating custom row come to MainActivity.java and add the below code to it. Here we are generating dummy list of videos that we are getting from string.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
package com.youtubeandroidplayerapidemo.main; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import com.youtubeandroidplayerapidemo.R; import com.youtubeandroidplayerapidemo.adapter.YoutubeVideoAdapter; import com.youtubeandroidplayerapidemo.model.YoutubeVideoModel; import com.youtubeandroidplayerapidemo.utils.RecyclerViewOnClickListener; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setUpRecyclerView(); populateRecyclerView(); } /** * setup the recyclerview here */ private void setUpRecyclerView() { recyclerView = findViewById(R.id.recycler_view); recyclerView.setHasFixedSize(true); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(linearLayoutManager); } /** * populate the recyclerview and implement the click event here */ private void populateRecyclerView() { final ArrayList<YoutubeVideoModel> youtubeVideoModelArrayList = generateDummyVideoList(); YoutubeVideoAdapter adapter = new YoutubeVideoAdapter(this, youtubeVideoModelArrayList); recyclerView.setAdapter(adapter); //set click event recyclerView.addOnItemTouchListener(new RecyclerViewOnClickListener(this, new RecyclerViewOnClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { //start youtube player activity by passing selected video id via intent startActivity(new Intent(MainActivity.this, YoutubePlayerActivity.class) .putExtra("video_id", youtubeVideoModelArrayList.get(position).getVideoId())); } })); } /** * method to generate dummy array list of videos * * @return */ private ArrayList<YoutubeVideoModel> generateDummyVideoList() { ArrayList<YoutubeVideoModel> youtubeVideoModelArrayList = new ArrayList<>(); //get the video id array, title array and duration array from strings.xml String[] videoIDArray = getResources().getStringArray(R.array.video_id_array); String[] videoTitleArray = getResources().getStringArray(R.array.video_title_array); String[] videoDurationArray = getResources().getStringArray(R.array.video_duration_array); //loop through all items and add them to arraylist for (int i = 0; i < videoIDArray.length; i++) { YoutubeVideoModel youtubeVideoModel = new YoutubeVideoModel(); youtubeVideoModel.setVideoId(videoIDArray[i]); youtubeVideoModel.setTitle(videoTitleArray[i]); youtubeVideoModel.setDuration(videoDurationArray[i]); youtubeVideoModelArrayList.add(youtubeVideoModel); } return youtubeVideoModelArrayList; } } |
7. For adding items to ArrayList we need a pojo class naming YoutubeVideoModel.java and add the below code to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package com.youtubeandroidplayerapidemo.model; /** * Created by sonu on 10/11/17. * class to set and get the video id, title and duration for a video */ public class YoutubeVideoModel { private String videoId, title,duration; public String getVideoId() { return videoId; } public void setVideoId(String videoId) { this.videoId = videoId; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDuration() { return duration; } public void setDuration(String duration) { this.duration = duration; } @Override public String toString() { return "YoutubeVideoModel{" + "videoId='" + videoId + '\'' + ", title='" + title + '\'' + ", duration='" + duration + '\'' + '}'; } } |
8. Now after creating pojo class lets create adapter class for RecyclerView with name of YoutubeVideoAdapter.java and add the below code to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
package com.youtubeandroidplayerapidemo.adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.google.android.youtube.player.YouTubeInitializationResult; import com.google.android.youtube.player.YouTubeThumbnailLoader; import com.google.android.youtube.player.YouTubeThumbnailView; import com.youtubeandroidplayerapidemo.utils.Constants; import com.youtubeandroidplayerapidemo.R; import com.youtubeandroidplayerapidemo.model.YoutubeVideoModel; import com.youtubeandroidplayerapidemo.holder.YoutubeViewHolder; import java.util.ArrayList; /** * Created by sonu on 10/11/17. */ public class YoutubeVideoAdapter extends RecyclerView.Adapter<YoutubeViewHolder> { private static final String TAG = YoutubeVideoAdapter.class.getSimpleName(); private Context context; private ArrayList<YoutubeVideoModel> youtubeVideoModelArrayList; public YoutubeVideoAdapter(Context context, ArrayList<YoutubeVideoModel> youtubeVideoModelArrayList) { this.context = context; this.youtubeVideoModelArrayList = youtubeVideoModelArrayList; } @Override public YoutubeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); View view = layoutInflater.inflate(R.layout.youtube_video_custom_layout, parent, false); return new YoutubeViewHolder(view); } @Override public void onBindViewHolder(YoutubeViewHolder holder, final int position) { final YoutubeVideoModel youtubeVideoModel = youtubeVideoModelArrayList.get(position); holder.videoTitle.setText(youtubeVideoModel.getTitle()); holder.videoDuration.setText(youtubeVideoModel.getDuration()); /* initialize the thumbnail image view , we need to pass Developer Key */ holder.videoThumbnailImageView.initialize(Constants.DEVELOPER_KEY, new YouTubeThumbnailView.OnInitializedListener() { @Override public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, final YouTubeThumbnailLoader youTubeThumbnailLoader) { //when initialization is sucess, set the video id to thumbnail to load youTubeThumbnailLoader.setVideo(youtubeVideoModel.getVideoId()); youTubeThumbnailLoader.setOnThumbnailLoadedListener(new YouTubeThumbnailLoader.OnThumbnailLoadedListener() { @Override public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) { //when thumbnail loaded successfully release the thumbnail loader as we are showing thumbnail in adapter youTubeThumbnailLoader.release(); } @Override public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) { //print or show error when thumbnail load failed Log.e(TAG, "Youtube Thumbnail Error"); } }); } @Override public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) { //print or show error when initialization failed Log.e(TAG, "Youtube Initialization Failure"); } }); } @Override public int getItemCount() { return youtubeVideoModelArrayList != null ? youtubeVideoModelArrayList.size() : 0; } } |
In the above class the main code which is responsible to show the thumbnail of video is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/* initialize the thumbnail image view , we need to pass Developer Key */ holder.videoThumbnailImageView.initialize(Constants.DEVELOPER_KEY, new YouTubeThumbnailView.OnInitializedListener() { @Override public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, final YouTubeThumbnailLoader youTubeThumbnailLoader) { //when initialization is sucess, set the video id to thumbnail to load youTubeThumbnailLoader.setVideo(youtubeVideoModel.getVideoId()); youTubeThumbnailLoader.setOnThumbnailLoadedListener(new YouTubeThumbnailLoader.OnThumbnailLoadedListener() { @Override public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) { //when thumbnail loaded successfully release the thumbnail loader as we are showing thumbnail in adapter youTubeThumbnailLoader.release(); } @Override public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) { //print or show error when thumbnail load failed Log.e(TAG, "Youtube Thumbnail Error"); } }); } @Override public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) { //print or show error when initialization failed Log.e(TAG, "Youtube Initialization Failure"); } }); |
9. I am keeping the Youtube API Key in Constants.java class that we got from Google Developer Console.
1 2 3 4 5 6 7 8 9 10 11 |
package com.youtubeandroidplayerapidemo.utils; /** * Created by sonu on 10/11/17. */ public class Constants { //youtube developer API Key public static String DEVELOPER_KEY = "YOUR_DEVELOPER_API_KEY"; } |
10. For RecyclerView holder class lets create a class with name YoutubeViewHolder.java and find the id of all the views.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.youtubeandroidplayerapidemo.holder; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import com.google.android.youtube.player.YouTubeThumbnailView; import com.youtubeandroidplayerapidemo.R; /** * Created by sonu on 10/11/17. */ public class YoutubeViewHolder extends RecyclerView.ViewHolder { public YouTubeThumbnailView videoThumbnailImageView; public TextView videoTitle, videoDuration; public YoutubeViewHolder(View itemView) { super(itemView); videoThumbnailImageView = itemView.findViewById(R.id.video_thumbnail_image_view); videoTitle = itemView.findViewById(R.id.video_title_label); videoDuration = itemView.findViewById(R.id.video_duration_label); } } |
Till here we are done with YoutubeThumbnailView. Now lets see how to play a selected video using YoutubePlayerAPI.
11. To play a youtube video when user select any item from RecyclerView lets create one more xml layout naming youtube_player_activity.xml and add the com.google.android.youtube.player.YouTubePlayerView which is responsible to play the video.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" tools:context=".main.YoutubePlayerActivity"> <!-- youtube player view to play videos --> <com.google.android.youtube.player.YouTubePlayerView android:id="@+id/youtube_player_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> |
12. Now lets create YoutubePlayerActivity.java activity where we will play the youtube video.
NOTE : You need to extend Activity with YoutubeBaseActivity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
package com.youtubeandroidplayerapidemo.main; import android.os.Bundle; import android.support.annotation.Nullable; import android.util.Log; import android.view.View; import com.google.android.youtube.player.YouTubeBaseActivity; import com.google.android.youtube.player.YouTubeInitializationResult; import com.google.android.youtube.player.YouTubePlayer; import com.google.android.youtube.player.YouTubePlayerView; import com.youtubeandroidplayerapidemo.R; import com.youtubeandroidplayerapidemo.utils.Constants; /** * Created by sonu on 10/11/17. * <p> * ### Here you need to extend the activity with YouTubeBaseActivity otherwise it will crash the app ### */ public class YoutubePlayerActivity extends YouTubeBaseActivity { private static final String TAG = YoutubePlayerActivity.class.getSimpleName(); private String videoID; private YouTubePlayerView youTubePlayerView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.youtube_player_activity); //get the video id videoID = getIntent().getStringExtra("video_id"); youTubePlayerView = findViewById(R.id.youtube_player_view); initializeYoutubePlayer(); } /** * initialize the youtube player */ private void initializeYoutubePlayer() { youTubePlayerView.initialize(Constants.DEVELOPER_KEY, new YouTubePlayer.OnInitializedListener() { @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) { //if initialization success then load the video id to youtube player if (!wasRestored) { //set the player style here: like CHROMELESS, MINIMAL, DEFAULT youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT); //load the video youTubePlayer.loadVideo(videoID); //OR //cue the video //youTubePlayer.cueVideo(videoID); //if you want when activity start it should be in full screen uncomment below comment // youTubePlayer.setFullscreen(true); //If you want the video should play automatically then uncomment below comment // youTubePlayer.play(); //If you want to control the full screen event you can uncomment the below code //Tell the player you want to control the fullscreen change /*player.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT); //Tell the player how to control the change player.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() { @Override public void onFullscreen(boolean arg0) { // do full screen stuff here, or don't. Log.e(TAG,"Full screen mode"); } });*/ } } @Override public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) { //print or show error if initialization failed Log.e(TAG, "Youtube Player View initialization failed"); } }); } } |
Youtube Player is having three different type of Player Style:
2. Minimal Player Style : In this style we only get Play/Pause button with showing video progress at bottom.
3. Chromeless Player Style : This style doesn’t contain any controls.
13. Don’t forget to give Internet permission in your AndroidManifest.xml file else your app will not run.
1 |
<uses-permission android:name="android.permission.INTERNET" /> |
14. Finally all done, now you can also make your app where you can play Videos from Youtube.
Thanks.
Subscribe to us and get the latest news.
27 Comments
Marina
Tuesday, May 15th, 2018Hi. Help please. tools: context=”.MainActivity” – good, and tools:context=”.Youtube Player Activity ” > underlined. How to fix?
Dr. Droid
Tuesday, May 15th, 2018Hi Marina,
You can remove that tools:context its not that important.
Thanks
Marina
Tuesday, May 15th, 2018Thank you for the answer! The code worked.
Marina
Tuesday, May 15th, 2018But the video does not show…
Dr. Droid
Tuesday, May 15th, 2018Hi Marina,
Can you tell me what is the exact issue?
According to me the issues can be:
1. Invalid API_Key
2. Invalid SHA1 Fingerprint.
Please cross check this.
Thanks
Marina
Thursday, May 24th, 2018Hello! Everything works!!!! Thank you very much for this lesson! I couldn’t do it because the user must have a YouTube app on their phone. If YouTube is available, the picture is displayed. I am from Russia, so forgive the mistakes in my answer.
Sonam
Tuesday, September 11th, 2018Its not working. Pls help
Dr. Droid
Monday, September 17th, 2018Hi Sonam,
Can you tell me what exactly is not working.
And please cross check if you miss something.
Thanks
Mark Steven
Monday, October 1st, 2018Are there any solution for All the videos of you tube channel.I mean I want to play all the videos of the you tube channel.How to do it with channel ID ?Thank you
Dr. Droid
Monday, October 8th, 2018Hi Mark,
Please follow the Youtube Docs : https://developers.google.com/youtube/android/player/
Here you will get each and every stuff that you can do with Youtube APIs.
Thanks
Mark
Monday, October 1st, 2018Are there any solution for All videos.I mean How to add youtube channel all videos just like above example.Thank you
armaan
Wednesday, October 3rd, 2018videos are showing in list but does not play why?
Dr. Droid
Monday, October 8th, 2018Hi Armaan,
Please follow the tutorial properly and make sure replacing the API Keys with your own.
Thanks
Nia Arora
Friday, October 26th, 2018How you implemented click event on recycler view items? We need to create interface for that?
Dr. Droid
Friday, October 26th, 2018Hi Nia,
Yes there is an interface for Click Event. Download the code and you will find the interface class in it.
Thanks
Navya
Friday, October 26th, 2018Hi,
You have less videos so you created array in strings. But I have 150 videos, How can I do that?. I need to create array for all video names?
Dr. Droid
Friday, October 26th, 2018Hi Navya,
You can create array also but that is not good approach. Instead you can use YoutubeAPIs to get all videos on your channel.
Please follow the Youtube API Docs : https://developers.google.com/youtube/android/player/
Thanks
Jhamil
Saturday, November 17th, 2018Hi, can you also automate video infos i saw a example somewhere which where you can obtain video duration title channel name descriptions and more using the api itself and the collected data will be displayed for textviews
Dr. Droid
Sunday, November 18th, 2018Hi Jhamil,
Yes it can be done but as i have only shown a demo with basic info. There are lot in Youtube APIs.
Please follow the Youtube API Docs : https://developers.google.com/youtube/android/player/
Thanks
Roman Calidt
Monday, December 24th, 2018it gives me error does not initialize YouTube player
Dr. Droid
Wednesday, December 26th, 2018Hi Roman,
Please make sure you have initialised Youtube with YOUR_DEVELOPER_KEY (not mine).
Thanks
Aunkit
Wednesday, January 2nd, 2019Hi sir. Amazing work by you. Thanks a ton. Everything works fine but when I tap on the the items . It says “An error occured while initializing the YouTube player.”
I’ve downloaded your source code and I’m directly running the app from there. I’ve also replaced your API key with mine. And I also am testing it on a real device where the Youtube app is already present.
Dr. Droid
Wednesday, January 2nd, 2019Hi Aunkit,
First of all Thank You!!
Can you confirm whether you have enabled Youtube API at Google Developer Console?
Thanks
Ethan Kim
Saturday, April 27th, 2019Hello! I was just going by through your tutorial, and I wanted to ask you a question:
I have created a similar app like this tutorial and used the same method as yours to create a recycler view. but instead of using a manual list of video IDs, I bring the title and video of a youtube channel’s uploaded videos using the Youtube Data API v3. However, I have noticed a problem.
When you scroll very fast or reload the activity by rotating the screen. I have realized that there is a java exception error:
[java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.]
This may be because the client device is not connected to the API when the application is loading other processes. Do you know a way to make sure the client is always connected to the API? Thank you!
Please feel free to contact me with the email.
Priyanka
Saturday, May 30th, 2020Hii Dr. Droid,
Can we play videos in recycler view only not another activity or fragment?
Dr. Droid
Friday, June 5th, 2020Hi Priyanka,
Yes, we can do it. Some links for your help:
https://github.com/obipawan/youtube-recyclerview
https://stackoverflow.com/questions/42018434/playing-youtube-video-using-recycler-view
Thanks
Akshay Ashok
Wednesday, March 3rd, 2021Hi David,
I changed the developer_API_key string to some meaningless text instead of putting the actual API key and everything is still working perfectly! How??
Thank you.