返回包含MediaPlayer的Activity时出现IllegalStateException

时间:2014-11-13 15:21:33

标签: android android-mediaplayer surfaceview surfaceholder

我有一个带有MediaPlayer的Activity,每当我从中启动另一个Activity(例如Chromecast Controller Activity或使用ShareActionProvider)并返回带有MediaPlayer的Activity时,我会收到以下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)

阅读文档说它可以在任何状态下调用setDisplay()。

我的相关代码如下:

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 个答案:

答案 0 :(得分:1)

好吧,你做mPlayer.release();在onPause()方法中,所以mPlayer被释放,你就无法对他做任何事情。尝试重新启动它或不在onPause中释放。例如,尝试在onStop中执行此操作。

答案 1 :(得分:1)

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

在bundle.getExtras()之上检查

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

确保始终初始化