Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically skip video in youtube when error occurs using YouTube Android SDK

I am trying to build an app that plays a list of videos. Its a very basic implementation based on the API samples.

The playlist (array of video_ids) gets loaded using player.loadVideos method. Changes in the player state are tracked using an implementation of PlayerStateChangeListener.

Everything works great, and the videos play back-to-back (as expected).

When there is an error in playing the video, (eg. YouTubePlayer.ErrorReason.NOT_PLAYABLE), it gets captured in the onError method of PlayerStateChangeListener, and I call player.next() there to move on to the next video.

However, player.next does not work after there is an error - basically once there is an error, there is nothing that I can do that would skip that video and play the next one that is queued up in the playlist.

like image 237
Toofan Avatar asked Aug 29 '13 10:08

Toofan


1 Answers

When a player error occurs while YouTubePlayer plays a playlist, calling next() has no effect. I believe this is because errors in YouTubePlayer are non-recoverable. The solution is to reload the playlist with the next playlist index.

So, to keep track of the current playlist index,

@Override   // YouTubePlayer.PlaylistEventListener
public void onPrevious() 
{
    mPlaylistIndex--;
}

@Override   // YouTubePlayer.PlaylistEventListener
public void onNext() 
{
    mPlaylistIndex++;
}

Then, in onError(),

@Override   // YouTubePlayer.PlayerStateChangeListener
public void onError(YouTubePlayer.ErrorReason errorReason) 
{
    Log.d(TAG, "YouTubePlayer error: " + errorReason);

    // Check if the mContentId is of kind video or playlist,
    // if video, there is nothing you can do, end playback.

    // Check if the mPlaylistIndex is the last video in playlist,
    // if so, there is nothing you can do, end playback.

    mPlaylistIndex++;
    mYouTubePlayer.loadPlaylist(mContentId, mPlaylistIndex, 0);
}
like image 135
Tony Avatar answered Oct 20 '22 14:10

Tony