IllegalStateException in MediaPlayer set datasource method

If your MediaPlayer is not playing anything or track is over, it will not be reset. Take your mMediaPlayer.reset(); out of if condition

try {
    mMediaPlayer.reset();
    mMediaPlayer.setDataSource(MainActivity.localTrackList.get(MainActivity.currentOffset).getPath());
    mMediaPlayer.prepareAsync();
} catch (IOException e) {
    e.printStackTrace();
}

You should call reset() before using setDataSource().

Your condition is absolutely write if media is playing, but it went false when track is over.


MediaPlayer.setDataSource() is only useful when the MediaPlayer's state is Idle, you called this method in onCompletion so the android system gave you a IllStateException. mMediaPlayer.isPlaying() seems always return false in onCompletion method.

Actually you do not need to setDataSource again, you can just call MediaPlayer.start() to play the data from start.

See this state diagram (taken from https://developer.android.com/reference/android/media/MediaPlayer.html#StateDiagram):

Tags:

Android