媒体播放器prepareAsync()非法状态异常Android

时间:2016-08-17 06:39:49

标签: android

这是我的Videoplayer类代码,每次我回家都给出了一个例外,然后直接回到这个活动,我试过但是找不到解决方案,    错误:

java.lang.IllegalStateException
  at android.media.MediaPlayer.prepareAsync(Native Method)
  at com.shaw.wind.predict2win.VideoPlayerActivity.surfaceCreated(VideoPlayerActivity.java:85)
  at android.view.SurfaceView.updateWindow(SurfaceView.java:600)
  at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:243)
  at android.view.View.dispatchWindowVisibilityChanged(View.java:9122)
  at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1170)
  at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1170)
  at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1170)
  at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1170)
  at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1170)
  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1330)
  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1073)
  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5988)
  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
  at android.view.Choreographer.doCallbacks(Choreographer.java:580)
  at android.view.Choreographer.doFrame(Choreographer.java:550)
  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
  at android.os.Handler.handleCallback(Handler.java:739)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:135)
  at android.app.ActivityThread.main(ActivityThread.java:5930)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

和代码:

public void surfaceCreated(SurfaceHolder holder) {
        player.setDisplay(holder);
        player.prepareAsync();
    }

(player is object of mediaplayer)

2 个答案:

答案 0 :(得分:2)

检查表面视图是否被破坏。在这种情况下,尝试在surface destroy方法中释放资源:

    if (mMediaPlayer != null) {
        mMediaPlayer.reset();
        mMediaPlayer.release();
        mMediaPlayer = null;
    }

在活动恢复时重新初始化媒体播放器。

答案 1 :(得分:0)

找到解决方案,重新初始化onRestart()中的变量help.But仍然不确定究竟是什么问题

@Override
public void onRestart(){
  super.onRestart();
  player = new MediaPlayer();
  videoSurface = (SurfaceView) findViewById(R.id.videoSurface);
  SurfaceHolder videoHolder = videoSurface.getHolder();
  videoHolder.addCallback(this);
  controller = new VideoControllerView(this);
}