Android Youtube Player, have you ever tried integrating Youtube Player into your app? yes its possible by integration of play youtube api into your application also make sure that the video you are integrating need to have embed permission.
Optimal implementation guide for Android YouTube Player integration in 2024: Step-by-step tutorial for seamless video playback within Android applications.
Android app has different types of presenting data to end user and video is one of its kind so in this tutorial we can help you out with integrating youtube video into your app.
Before going any further into tutorial lets get a api key from google console api Google Api Key
Android Youtube Player Video :
Explore the following YouTube tutorial for a detailed, step-by-step guide on implementing Android YouTube Player integration.
Create a credential using your SHA1 Algorithm need help in generating SHA1
then
API key is generated if required you can restrict your api key depending upon your usage level. Copy the api key for further usage in integrating youtube videos.
Add youtube library to your android studio projects
// Replace this with the video code you want to play
public static final String VIDEO_CODE = "videocode";
private static String getApiKey(){
// Load API key from a secure location (e.g., configuration file, environment variable)
// Alternatively, consider using a secure method to retrieve the key dynamically
return"Your_API_Key_Here";
}
}
public class Config {
// Google Console APIs API key
public static final String API_KEY = getApiKey();
// Replace this with the video code you want to play
public static final String VIDEO_CODE = "videocode";
private static String getApiKey() {
// Load API key from a secure location (e.g., configuration file, environment variable)
// Alternatively, consider using a secure method to retrieve the key dynamically
return "Your_API_Key_Here";
}
}
public class Config {
// Google Console APIs API key
public static final String API_KEY = getApiKey();
// Replace this with the video code you want to play
public static final String VIDEO_CODE = "videocode";
private static String getApiKey() {
// Load API key from a secure location (e.g., configuration file, environment variable)
// Alternatively, consider using a secure method to retrieve the key dynamically
return "Your_API_Key_Here";
}
}
YoutubeActivity.java
Now initialize Youtube Player, EventListener & StateChangeListener in your activity class
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class YoutubeAdd extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{
private static final int RECOVERY_DIALOG_REQUEST = 1;
private YouTubePlayerView playerView;
private EventListener eventListener;
private StateChangeListener stateChangeListener;
public class YoutubeAdd extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
private static final int RECOVERY_DIALOG_REQUEST = 1;
private YouTubePlayerView playerView;
private EventListener eventListener;
private StateChangeListener stateChangeListener;
public class YoutubeAdd extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
private static final int RECOVERY_DIALOG_REQUEST = 1;
private YouTubePlayerView playerView;
private EventListener eventListener;
private StateChangeListener stateChangeListener;
then
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
eventListener = newEventListener();
stateChangeListener = newStateChangeListener();
playerView = findViewById(R.id.player_view);
playerView.initialize(Config.API_KEY, this);
eventListener = new EventListener();
stateChangeListener = new StateChangeListener();
playerView = findViewById(R.id.player_view);
playerView.initialize(Config.API_KEY, this);
eventListener = new EventListener();
stateChangeListener = new StateChangeListener();
playerView = findViewById(R.id.player_view);
playerView.initialize(Config.API_KEY, this);
If the initialization encounters an error, then
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@Override
public voidonInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorResult){
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
player.setPlaybackEventListener(eventListener);
player.setPlayerStateChangeListener(stateChangeListener);
if (!wasRestored) {
player.cueVideo(Config.VIDEO_CODE);
player.setPlayerStyle(PlayerStyle.DEFAULT);
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
player.setPlaybackEventListener(eventListener);
player.setPlayerStateChangeListener(stateChangeListener);
if (!wasRestored) {
player.cueVideo(Config.VIDEO_CODE);
player.setPlayerStyle(PlayerStyle.DEFAULT);
}
}
Using EventListener we can track video whether video is being played, paused, stopped, buffering and also seek
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
private final class EventListener implements YouTubePlayer.PlaybackEventListener{
@Override
public voidonPlaying(){
Log.e("Status", "Playing");
}
@Override
public voidonPaused(){
Log.e("Status", "Paused");
}
@Override
public voidonStopped(){
Log.e("Status", "Stopped");
}
@Override
public voidonBuffering(boolean isBuffering){}
@Override
public voidonSeekTo(int positionMillis){}
}
private final class EventListener implements YouTubePlayer.PlaybackEventListener {
@Override
public void onPlaying() {
Log.e("Status", "Playing");
}
@Override
public void onPaused() {
Log.e("Status", "Paused");
}
@Override
public void onStopped() {
Log.e("Status", "Stopped");
}
@Override
public void onBuffering(boolean isBuffering) { }
@Override
public void onSeekTo(int positionMillis) { }
}
private final class EventListener implements YouTubePlayer.PlaybackEventListener {
@Override
public void onPlaying() {
Log.e("Status", "Playing");
}
@Override
public void onPaused() {
Log.e("Status", "Paused");
}
@Override
public void onStopped() {
Log.e("Status", "Stopped");
}
@Override
public void onBuffering(boolean isBuffering) { }
@Override
public void onSeekTo(int positionMillis) { }
}
and using StateChangeListener we can know whether video is loaded, any error occurred or whether the ad started in between the video and most importantly whether the video is ended using which we can do further actions.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
private final class StateChangeListener implements YouTubePlayer.PlayerStateChangeListener{
@Override
public voidonLoading(){}
@Override
public voidonLoaded(String videoId){}
@Override
public voidonAdStarted(){}
@Override
public voidonVideoStarted(){}
@Override
public voidonVideoEnded(){}
@Override
public voidonError(YouTubePlayer.ErrorReason errorReason){}
}
private final class StateChangeListener implements YouTubePlayer.PlayerStateChangeListener {
@Override
public void onLoading() { }
@Override
public void onLoaded(String videoId) { }
@Override
public void onAdStarted() { }
@Override
public void onVideoStarted() { }
@Override
public void onVideoEnded() { }
@Override
public void onError(YouTubePlayer.ErrorReason errorReason) { }
}
private final class StateChangeListener implements YouTubePlayer.PlayerStateChangeListener {
@Override
public void onLoading() { }
@Override
public void onLoaded(String videoId) { }
@Override
public void onAdStarted() { }
@Override
public void onVideoStarted() { }
@Override
public void onVideoEnded() { }
@Override
public void onError(YouTubePlayer.ErrorReason errorReason) { }
}
If you have any questions regarding the Youtube Player tutorial, feel free to share them in the comment section below. If you find this tutorial helpful, don’t forget to give it a like and share it.