nullByte
nullByte

Reputation: 53

media player prepareAsync() illegal state exception Android

This is my Videoplayer class code,it gave an exception every time i go to home and then came back to this activity directly,i tried but cant find a soluion, 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)

Upvotes: 4

Views: 3287

Answers (2)

nullByte
nullByte

Reputation: 53

found the solution,re initialization of variables in onRestart() helped.But still not sure what exactly was problem

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

Upvotes: 0

random
random

Reputation: 10309

Check if surface view is getting destroyed. In that case try to release resources inside surface destroy method:

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

And reinitialise media player on activity resume.

Upvotes: 3

Related Questions