What means the message “internal/external state mismatch corrected” at the MediaPlayer?

主宰稳场 提交于 2019-12-10 22:05:15

问题


I work with a MediaPlayer and set the state of the player often programmatically like for example:

 if(mp.isPlaying()) {
    mp.pause();
    animationPausedMusic();

  }

private void animationPausedMusic() {

  // Changing button image to play button
    btn_play.setBackgroundResource(R.drawable.play);

  ... // more code
}

But sometimes the logcat gives me the message:

"internal/external state mismatch corrected"

And then the play and pause function is not working anymore.

What does this message mean? And how can I solve it?


回答1:


After going through the android's native framework for media player I found that in source file mediaplayer.cpp inside function bool MediaPlayer::isPlaying() The developer is checking if the currentState of media player is in STARTED state and yet the media player is not playing any media, so it tries to change the state to PAUSED state so that the state consistency should be maintained for API users.(and here is where he is printing the message "ALOGE("internal/external state mismatch corrected");")

Now If you go through the media player state diagram below:

You would notice that this may happen when the MediaPlayer moved to 'STARTED' state after a call to start() and at this time for some obscure reason has not yet started the playback and you fire a MediaPlayer.isPlaying() method call , The Framework treat this as state inconsistency and moves to 'PAUSED' state and that's why you cannot see anything playing further.

However, if someone has some better understanding please share your thoughts!




回答2:


I ran into this recently, and like some other questions say, it's this bug (marked obsolete alas) https://code.google.com/p/android/issues/detail?id=9732

I found this error occurs when playing a MIDI file, but only sometimes. It happens when mp.isPlaying() is called quickly after mp.start()

If you can manage to not call mp.isPlaying() for a little bit, the error doesn't occur. In my case, a 10th of a second or so made the difference between getting the error or not. It's awkward, but it works.

e.g.

//setting a new track
mp.setDataSource(path);
mp.prepare();
mp.start();
//calling mp.isPlaying() here or shortly after starts the problem
//since we know it's playing, we can store that state, or call
updateUiPlaying(); //eg instead of updateUi();
//or just call some code here that takes more time first
updateScaledImages(); //something that might take time
Log.v(TAG, "mp.isPlaying = " + mp.isPlaying()); //now isPlaying() shouldn't cause that error

Also, I put a check in when I pause later.

mp.pause()
if(mp.isPlaying()){
    //shouldn't be playing, must be in error
    mp.stop();
    mp.release();
    mp = new MediaPlayer();
    //any other initialization here

}

Though the problem doesn't occur if there is a wait before calling isPlaying()




回答3:


Apparently there is more than one cause of this message. The following solution worked for me. It may or may not work for you. I called the method MediaPlayer.reset() immediately after instantiating the MediaPlayer object:

    MediaPlayer mp = new MediaPlayer();
    mp.reset();


来源:https://stackoverflow.com/questions/26545205/what-means-the-message-internal-external-state-mismatch-corrected-at-the-media

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!