IllegalStateException when returning to Activity containing MediaPlayer

1.3k views Asked by At

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();
        }
    }
2

There are 2 answers

0
G_V On

Put

mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mPlayer = new MediaPlayer();
mController = new VideoControllerView(this, false);

above the bundle.getExtras() check like

mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mPlayer = new MediaPlayer();
mController = new VideoControllerView(this, false);

Bundle b = getIntent().getExtras();
        if (b != null) {
            mSelectedMedia = Utils.toMediaInfo(getIntent().getBundleExtra("media"));
            mURL = mSelectedMedia.getContentId();
        }

to ensure it's always initialized

1
Vladimir Ivanov On

Well, you do mPlayer.release(); in onPause() method, so mPlayer is released and you just can't do anything with him. Try to reinit it or do not release in onPause. Try to do it in onStop for example.