I have an Activity with a MediaPlayer and whenever I launch another Activity from it (for example Chromecast Controller Activity or using a ShareActionProvider) and return to the Activity with the MediaPlayer, I get the following IllegalStateException:
11-13 15:48:33.083 22342-22342/com.iosharp.android.ssplayer E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException
at android.media.MediaPlayer._setVideoSurface(Native Method)
at android.media.MediaPlayer.setDisplay(MediaPlayer.java:733)
at com.iosharp.android.ssplayer.videoplayer.VideoActivity.surfaceCreated(VideoActivity.java:126)
at android.view.SurfaceView.updateWindow(SurfaceView.java:610)
at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:239)
at android.view.View.dispatchWindowVisibilityChanged(View.java:8170)
Reading the documentation it says setDisplay() can be called in any state.
My relevant code below:
public class VideoActivity extends ActionBarActivity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener,
VideoControllerView.MediaPlayerControl, MediaPlayer.OnErrorListener {
private SurfaceView mSurfaceView;
private MediaPlayer mPlayer;
private VideoControllerView mController;
private String mURL;
private SurfaceHolder mSurfaceHolder;
private VideoCastManager mCastManager;
private MediaInfo mSelectedMedia;
private IVideoCastConsumer mVideoCastConsumer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCastManager = CastApplication.getCastManager(this);
setContentView(R.layout.activity_video);
mSurfaceView = (SurfaceView) findViewById(R.id.videoSurface);
setupActionBar();
setupCastListeners();
Bundle b = getIntent().getExtras();
if (b != null) {
mSelectedMedia = Utils.toMediaInfo(getIntent().getBundleExtra("media"));
mURL = mSelectedMedia.getContentId();
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mPlayer = new MediaPlayer();
mController = new VideoControllerView(this, false);
}
}
@Override
protected void onResume() {
super.onResume();
setupLocalPlayback();
if (mCastManager != null) {
mCastManager.incrementUiCounter();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mController.show();
showActionBar();
return false;
}
@Override
public void onBackPressed() {
super.onBackPressed();
// Hide to prevent illegal state exception of getCurrentPosition
mController.hide();
if (mPlayer != null) {
mPlayer.reset();
}
}
@Override
protected void onPause() {
super.onPause();
mController.hide();
mPlayer.release();
if (mCastManager != null) {
mCastManager.decrementUiCounter();
}
}
Put
above the bundle.getExtras() check like
to ensure it's always initialized