Reputation: 89
I have a Youtube player within my app. I have set a setPlayerStateChangeListener for this player, and on the event of the video ending (onVideoEnd()) I wish to play the next cued video.
This is what I have so far...
YouTubeFragment
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean restored) {
...
player.setPlayerStateChangeListener(myPlayerStateChangeListener);
...
}
MyPlayerStateChangeListener
package x;
import android.util.Log;
import com.google.android.youtube.player.YouTubePlayer;
public final class MyPlayerStateChangeListener implements YouTubePlayer.PlayerStateChangeListener {
@Override
public void onAdStarted() {
}
@Override
public void onError(
com.google.android.youtube.player.YouTubePlayer.ErrorReason arg0) {
}
@Override
public void onLoaded(String arg0) {
}
@Override
public void onLoading() {
}
@Override
public void onVideoEnded() {
Log.d("onVideoEnded()", "Video has ended");
}
@Override
public void onVideoStarted() {
}
}
When the video ends, the log message within onVideoEnded() successfully displays. What I am struggling to get my head around is how I can run hasNext(), play() etc on my youtube player from within this PlayerStateChangeListener.
Upvotes: 0
Views: 213
Reputation: 498
Actually you can use the following :
private static YouTubePlayer player;
In onInitializationSuccess() set this variable.
this.player = player;
Now in the callback onVideoEnd() use the static version of the player to perform the actions that you need like cueing a new video.
Upvotes: 1