Rapid Subscribe Android App

Rapid Subscribe Android App
Rapid Subscriber

Recent Posts

YouTube Data Api V3 Event Listeners

YouTube Data Api V3 Event Listeners






For Youtube Data Api V3 Creation and Initialization click here

For Youtube Data Api V3 detailed methods click here

For creating seek to millis(jump to specific location of video) click here

In this blog, we will use 3 listeners to control out youtube player view.

  1. MyPlayerStateChangeListener
  2. MyPlaybackEventListener
  3. MyPlaylistEventListener
By Implementing these listeners, we can check the status and response from our Youtube Player View,

Use of Listeners in Java File.

activity.java
 package studio.harpreet.sampleyoutubedataapi;  
   
 import androidx.appcompat.app.AppCompatActivity;  
   
 import android.content.Intent;  
 import android.os.Bundle;  
 import android.util.Log;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.EditText;  
 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.YouTubePlayerView;  
   
 import java.util.ArrayList;  
   
 import static android.content.ContentValues.TAG;  
   
 public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {  
   
   YouTubePlayerView youTubePlayerView;  
   
   MyPlayBackEventListener playBackEventListener;  
   MyPlayerStateChangeListener playerStateChangeListener;  
   
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
   
     youTubePlayerView = findViewById(R.id.youtube_player);  
     youTubePlayerView.initialize(getString(R.string.youtube_api_key),this);  
   
     playerStateChangeListener = new MyPlayerStateChangeListener();  
     playBackEventListener = new MyPlayBackEventListener();  
   
   }  
   
   @Override  
   public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {  
   
     youTubePlayer.setPlaybackEventListener(playBackEventListener);  
     youTubePlayer.setPlayerStateChangeListener(playerStateChangeListener);  
   
     if(!b)  
     {  
       //youTubePlayer.cueVideo("-_pacR6syDo"); //https://www.youtube.com/watch?v=-_pacR6syDo   
   }  
   
   @Override  
   public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {  
     if (youTubeInitializationResult.isUserRecoverableError()) {  
       youTubeInitializationResult.getErrorDialog(this, 1).show();  
     } else {  
       String error = String.format("Error initializing YouTube Player ", youTubeInitializationResult.toString());  
       Toast.makeText(this, error, Toast.LENGTH_LONG).show();  
     }  
   }  
   
   @Override  
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
     if(requestCode == 1)  
     {  
       getYoutubePlayerProvider().initialize(getString(R.string.youtube_api_key),this);  
     }  
   }  
   
   private YouTubePlayer.Provider getYoutubePlayerProvider() {  
   
     return youTubePlayerView;  
   
   }  
   
   private final class MyPlayBackEventListener implements YouTubePlayer.PlaybackEventListener  
   {  
   
     @Override  
     public void onPlaying() {  
       showMessage("onPlaying");  
     }  
   
     @Override  
     public void onPaused() {  
       showMessage("OnPaused");  
     }  
   
     @Override  
     public void onStopped() {  
       showMessage("OnStopped");  
     }  
   
     @Override  
     public void onBuffering(boolean b) {  
       showMessage("onBuffering");  
     }  
   
     @Override  
     public void onSeekTo(int i) {  
       showMessage("onSeekTo");  
     }  
   }  
   
   private final class MyPlayerStateChangeListener implements YouTubePlayer.PlayerStateChangeListener  
   {  
   
     @Override  
     public void onLoading() {  
       showMessage("onLoading");  
     }  
   
     @Override  
     public void onLoaded(String s) {  
       showMessage("onLoaded");  
     }  
   
     @Override  
     public void onAdStarted() {  
       showMessage("onAdStarted");  
     }  
   
     @Override  
     public void onVideoStarted() {  
       showMessage("onVideoStarted");  
     }  
   
     @Override  
     public void onVideoEnded() {  
       showMessage("onVideoEnded");  
     }  
   
     @Override  
     public void onError(YouTubePlayer.ErrorReason errorReason) {  
   
       showMessage("onError: "+errorReason);  
     }  
   }  
   
   private final class MyPlaylistEventListener implements YouTubePlayer.PlaylistEventListener // used only when we play a playlist  
   {  
   
     @Override  
     public void onPrevious() {  
   
     }  
   
     @Override  
     public void onNext() {  
   
     }  
   
     @Override  
     public void onPlaylistEnded() {  
   
     }  
   }  
   
   private void showMessage(String message)  
   {  
     Toast.makeText(this, message, Toast.LENGTH_SHORT).show();  
   }  
   
 }  
After run, We can see Toast messages of everything happened with our YouTube player view loaded video.


 
Follow us for more posts like this, 
Subscribe to Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

2 comments:

  1. Regardless of whether you need to make a video blog of yourself or then again assuming you and your companions need to simply mess around with a camcorder, you can do as such. https://www.buyyoutubeviewsindia.in/youtube-marketing/

    ReplyDelete
  2. Looking forward to reading more. Great blog post. Really looking forward to read more. Great.
    event management company in singapore

    ReplyDelete