Media player prepareAsync () illegal state Android exception

This is my Videoplayer class code, it threw an exception every time I returned home, and then returned to this activity directly, I tried, but could not find a solution, Error:

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)

and code:

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

(player is object of mediaplayer)
+4
source share
2 answers

Check if the appearance of the surface is damaged. In this case, try to free the resources inside the surface destruction method:

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

And reinitialize the media player on the resumption of activity.

+1
source

found a solution, reinitializing the variables in onRestart () helped. But still not sure what the problem was

@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);
}
0
source

Source: https://habr.com/ru/post/1651496/


All Articles