To handle actions after completing a video in a YouTube StandalonePlayer in Android, you can use the YouTubePlayer.PlayerStateChangeListener interface. The onVideoEnded() method of this interface will be called when the video playback is completed. In this method, you can perform any actions that you want to execute after the video is finished.

Here's a step-by-step guide on how to do this:

Step 1: Set up the YouTube API in your Android project.

  • Add the YouTube Android Player API to your project. You can do this by adding the dependency in your app's build.gradle file:
gradle
dependencies { implementation 'com.google.android.youtube:android-player:10.0.5' }

Step 2: Implement YouTubePlayer.PlayerStateChangeListener.

  • In your Activity or Fragment where you want to play the video, implement the YouTubePlayer.PlayerStateChangeListener interface.
java
import android.os.Bundle; import android.util.Log; 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; public class YoutubePlayerActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener, YouTubePlayer.PlayerStateChangeListener { private static final String YOUTUBE_VIDEO_ID = "YOUR_YOUTUBE_VIDEO_ID"; private YouTubePlayerView youTubePlayerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_youtube_player); youTubePlayerView = findViewById(R.id.youtube_player_view); youTubePlayerView.initialize("YOUR_YOUTUBE_API_KEY", this); } @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) { if (!wasRestored) { player.cueVideo(YOUTUBE_VIDEO_ID); player.setPlayerStateChangeListener(this); } } @Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) { if (errorReason.isUserRecoverableError()) { errorReason.getErrorDialog(this, 1).show(); } else { Log.e("YoutubePlayerActivity", "Error initializing YouTube player: " + errorReason.toString()); } } @Override public void onAdStarted() {} @Override public void onLoading() {} @Override public void onVideoStarted() {} @Override public void onVideoEnded() { // Perform actions after video is finished // For example, you can start another video, show a dialog, etc. } @Override public void onError(YouTubePlayer.ErrorReason errorReason) { Log.e("YoutubePlayerActivity", "Error playing YouTube video: " + errorReason.toString()); } }

In this example, the onVideoEnded() method is overridden to handle the actions that you want to perform after the video is finished.

Step 3: Add the YouTubePlayerView to your layout file.

  • In your layout XML file (activity_youtube_player.xml), add the YouTubePlayerView that will be used to display the video.
xml
<?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"> <com.google.android.youtube.player.YouTubePlayerView android:id="@+id/youtube_player_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" /> </RelativeLayout>

Step 4: Replace "YOUR_YOUTUBE_API_KEY" and "YOUR_YOUTUBE_VIDEO_ID" with your actual YouTube API key and the YouTube video ID you want to play in the YoutubePlayerActivity.

Now, when you run the YoutubePlayerActivity, it will play the specified YouTube video using the StandalonePlayer, and the onVideoEnded() method will be called after the video is completed. In the onVideoEnded() method, you can add the code to handle any actions you want to perform after the video playback is finished.

Have questions or queries?
Get in Touch