I have a ListView with videos as items. In some occasions I get an IllegalStateException while I try to set the surface. Source code:
if(surfaceTexture != null){
mSurface = new Surface(surfaceTexture);
mMediaPlayer.setSurface(mSurface);
}
Exception:
Fatal Exception: java.lang.IllegalStateException
android.media.MediaPlayer._setVideoSurface (MediaPlayer.java)
android.media.MediaPlayer.setSurface (MediaPlayer.java:829)
However, the Android MediaPlayer docs state that setSurface can be called in any state.
setSurface any {} This method can be called in any state and calling it does not change the object state.
(http://developer.android.com/reference/android/media/MediaPlayer.html)
I cannot reproduce this exception and I don't know in which state the MediaPlayer is during the exception.
Any Ideas?
This would occur if the player was not yet initialized or already released without your realizing it. The documentation for MediaPlayer doesn't seem to reflect the behavior I've experienced. I've encountered it throwing IllegalStateException on very early calls to setSurface where the mediaPlayer object is not null but likely not initialized and separately after release is called. To handle this I simply catch the exception:
try {
mediaPlayer.setSurface(surface);
} catch (Exception e) {
Log.i("TAG", "MediaPlayer setSurface failed.");
}
Since our callback that calls setSurface
gets hit multiple times throughout the setup of playback, by the time start
is called, the surface has been set again without throwing the exception.