Android Youtube Player :
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
Youtube Android Player API — Download
add it to libs folder of your project and integrate to
build.gradle(Module: app)
compile files('libs/YouTubeAndroidPlayerApi.jar')
Let’s start coding
activity_adview.xml
Just add YouTubePlayerView we don’t need any other view.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.youtube.player.YouTubePlayerView android:id="@+id/player_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="30dp" /> </RelativeLayout>
Config
Configure youtube player with the api key which is generated in above process and also youtube video which you want to play
https://androidcoding.in/2017/12/17/get-video-code-youtube-link/
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
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
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
@Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorResult) { if (errorResult.isUserRecoverableError()) { errorResult.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show(); } else { String error = String.format(getString(R.string.error_string), errorResult.toString()); Toast.makeText(this, error, Toast.LENGTH_LONG).show(); } }
and when succeeded
@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
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.
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) { } }
import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Window; import android.view.WindowManager; import android.widget.Toast; 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.YouTubePlayer.PlayerStyle; import com.google.android.youtube.player.YouTubePlayerView; 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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_adview); eventListener = new EventListener(); stateChangeListener = new StateChangeListener(); playerView = findViewById(R.id.player_view); playerView.initialize(Config.API_KEY, this); } @Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorResult) { if (errorResult.isUserRecoverableError()) { errorResult.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show(); } else { String error = String.format(getString(R.string.error_string), errorResult.toString()); Toast.makeText(this, error, Toast.LENGTH_LONG).show(); } } @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 protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == RECOVERY_DIALOG_REQUEST) { getYouTubePlayerProvider().initialize(Config.API_KEY, this); } } private YouTubePlayer.Provider getYouTubePlayerProvider() { return findViewById(R.id.player_view); } 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 b) { } @Override public void onSeekTo(int i) { } } private final class StateChangeListener implements YouTubePlayer.PlayerStateChangeListener { @Override public void onLoading() { } @Override public void onLoaded(String s) { } @Override public void onAdStarted() { } @Override public void onVideoStarted() { } @Override public void onVideoEnded() { } @Override public void onError(YouTubePlayer.ErrorReason errorReason) { } } }
AndroidManifest.xml
Add internet permission to manifest file to integrate Youtube Player
<uses-permission android:name="android.permission.INTERNET"/>
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.