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