Home > Enterprise >  Media player throwing illegal state exception
Media player throwing illegal state exception

Time:09-29

I'm trying to play a song but Media player is throwing IllegalState exception.This is how i'm trying to play a song

private fun playSong(songPath: String) {
        if (mediaPlayer != null) {
            if (mediaPlayer!!.isPlaying) {
                stopPlay()
                mediaPlayer!!.setDataSource(songPath)
                mediaPlayer!!.setOnPreparedListener(this)
                mediaPlayer!!.setOnCompletionListener(this)
                mediaPlayer!!.prepareAsync()
            } else {
                mediaPlayer!!.setDataSource(songPath)
                mediaPlayer!!.setOnPreparedListener(this)
                mediaPlayer!!.setOnCompletionListener(this)
                mediaPlayer!!.prepareAsync()
            }
        } else {
            mediaPlayer = MediaPlayer()
            mediaPlayer!!.setDataSource(songPath)
            mediaPlayer!!.setOnPreparedListener(this)
            mediaPlayer!!.setOnCompletionListener(this)
            mediaPlayer!!.prepareAsync()
        }

and this is how i'm stoping play

private fun stopPlay() {
        mediaPlayer!!.pause()
        mediaPlayer!!.stop()
    }

exception thrown by media player is

java.lang.IllegalStateException
        at android.media.MediaPlayer._setDataSource(Native Method)

CodePudding user response:

You should call reset() before setDataSource when you are re-using the same media player instance.

   if (mediaPlayer != null) {
        if (mediaPlayer!!.isPlaying) {
            stopPlay()
            mediaPlayer!!.reset()
            mediaPlayer!!.setDataSource(songPath)
            mediaPlayer!!.setOnPreparedListener(this)
            mediaPlayer!!.setOnCompletionListener(this)
            mediaPlayer!!.prepareAsync()
        } else {
            mediaPlayer!!.reset()
            mediaPlayer!!.setDataSource(songPath)
            mediaPlayer!!.setOnPreparedListener(this)
            mediaPlayer!!.setOnCompletionListener(this)
            mediaPlayer!!.prepareAsync()
        }
    } else {
        mediaPlayer = MediaPlayer()
        mediaPlayer!!.setDataSource(songPath)
        mediaPlayer!!.setOnPreparedListener(this)
        mediaPlayer!!.setOnCompletionListener(this)
        mediaPlayer!!.prepareAsync()
    }

CodePudding user response:

You need to reset media player before setDataSource and also set setOnErrorListener and handle error like reset media player.

private fun playSong(songPath: String) {
    if (mediaPlayer != null) {
        if (mediaPlayer?.isPlaying == true) {
            stopPlay()
        }
        mediaPlayer?.reset()
    } else {
        mediaPlayer = MediaPlayer()
    }
    mediaPlayer?.setDataSource(songPath)
    mediaPlayer?.setOnPreparedListener(this)
    mediaPlayer?.setOnCompletionListener(this)
    mediaPlayer?.setOnErrorListener(this)
    mediaPlayer?.prepareAsync()
}

override fun onError(mp: MediaPlayer, what: Int, extra: Int): Boolean {
    mp.reset()
}
  • Related