0

If I pause my activity, on returning I get a crash

Caused by: java.lang.IllegalStateException
at android.media.MediaPlayer.prepareAsync(Native Method)

I want to be able to return to exactly where the video left off and continue from there. I also want it to survive orientations, which it is not doing right now. Thanks for any hint on how to accomplish this. I am not using ExoPlayer because I want to stay with API 14, instead of bumping to API-16 and lose some users.

My setup is Activity -> Fragment {TextureView}. In portrait the fragment is half of the screen; in landscape the fragment is fullscreen.

code snippet

 @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
        surface = new Surface(surfaceTexture);
        playVideoNow();
    }

private void playVideoNow() {
    if(null== textureView || null==surface || null==mCurrVideo) return;
    try {
        final String url = mCurrVideo.getUrl();
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(getContext(), Uri.parse(url));
        mediaPlayer.setSurface(surface);
        mediaPlayer.setLooping(false);
        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer player) {
                mediaPlayer.start();
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
}

playVideoNow() is called from multiple possible places such as onResume on BroadcastReceiver, hence the checks to see if everything is in order. Also the

learner
  • 11,490
  • 26
  • 97
  • 169
  • must be doing some thing with onSaveInstance ? please post whole activity if possible. – dex Oct 18 '15 at 06:15
  • I am not doing anything with onSavedInstance of the Activity or the Fragment. The activity in question has two fragments. The video fragment, and a list fragment below the video fragment. the list fragment has onSaveInstanceState implemented, but there is no relation there. I can post the relevant snippet, but posting the whole activity and then the whole fragment is problematic. – learner Oct 18 '15 at 06:21

0 Answers0