IllegalStateException in MediaPlayer set datasource method

Related searches

I'm using the following code in my onCompletion listener of MediaPlayer where I'm repeating the current song.

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

But I'm getting the following error Message,

  at android.media.MediaPlayer._setDataSource(Native Method)
09-15 09:05:19.884 12545-12545/ W/System.err:     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1115)
09-15 09:05:19.884 12545-12545/ W/System.err:     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1100)
09-15 09:05:19.884 12545-12545/ W/System.err:     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1079)
09-15 09:05:19.884 12545-12545/ W/System.err:     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1028)

How can I be able to sort this out?

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):

java.lang.IllegalStateException in MediaPlayer.setDataSource , In my case, i had to call reset() on the MediaPlayer instance, before i supplied it with a different url. As far as the documentation goes,� As an alternative, the application could first open the file for reading, and then use the file descriptor form SetDataSource (FileDescriptor) . IOException. IOException. if it is called in an invalid state. When path refers to a local file, the file may actually be opened by a process other than the calling application.

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.

java.lang.IllegalStateException what does it mean?, Method.invoke(Method.java:521) 10-19 09:44:49.056: If you see this state diagram (taken from https://developer.android.com/reference/android/media/ MediaPlayer.html# Only then can you call setDataSource(). C# (CSharp) android.media MediaPlayer.setDataSource - 3 examples found. These are the top rated real world C# (CSharp) examples of android.media.MediaPlayer.setDataSource extracted from open source projects. You can rate examples to help us improve the quality of examples.

Lets suppose you have a list of songs and on click of song you have to play the song from file. You need to re initialize the MediaPlayer again. Setting isLooping is optional

Here file path is from internal dir "/system/media/audio/alarms/Osmium.ogg

fun onItemClick(item: FetchMedia.SongModel) {
    if(mediaPlayer.isPlaying){
        mediaPlayer.isLooping = false
        mediaPlayer.stop()
    }
    mediaPlayer = MediaPlayer()
    mediaPlayer.setDataSource(item.DATA)
    mediaPlayer.prepare()
    mediaPlayer.start()
    mediaPlayer.isLooping = true
}

java.lang.IllegalStateException MediaPlayer setDataSource, What do you mean with “this solution also fails”? Does it throw an IllegalStateException? From what I see, it just won’t do anything at all, because you register your OnPreparedListener after the MediaPlayer object has prepared itself, causing the onPrepared() method never to be called. A better approach would be to write:

MediaPlayer, Reported May 22, 2013 on release 0.9. java.lang.IllegalStateException at android.media.MediaPlayer._start(Native Method) at android.media.MediaPlayer.start

Home » Java » java.lang.IllegalStateException at android.media.MediaPlayer._prepare(Native Method) java.lang.IllegalStateException at android.media.MediaPlayer._prepare(Native Method) Posted by: admin October 24, 2018 Leave a comment

Sometimes onDestroy() is called before MediaPlayer gets prepared because I set device's orientation according to user's pre-defined option from the app's settings in onCreate() of the activity (and I start() the MediaPlayer in onCreate() as well), and you know, when the orientation is changed, the activity gets recreated.

Comments
  • prepareAsync is not working for me, whereas prepare is working. Can you explain why this is happening?