如果我暂停活动,返回时会发生崩溃
Caused by: java.lang.IllegalStateException
at android.media.MediaPlayer.prepareAsync(Native Method)
我希望能够准确地返回视频停止的位置并从那里继续。我也希望它能够幸存下来,而现在却没有这样做。感谢您提供有关如何实现此目标的任何提示。我没有使用ExoPlayer,因为我想继续使用API 14,而不是碰到API-16而失去一些用户。
我的设置为Activity -> Fragment {TextureView}
。在肖像中,片段是屏幕的一半;在横向中,片段是全屏的。
代码段
@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()
从BroadcastReceiver上的多个可能位置调用,例如onResume
,因此检查是否所有内容都是有序的。