每当我暂停我的活动(实际上是片段)去另一个应用程序时,在使用onResume返回时,我尝试恢复视频播放但不播放:我得到一个空白屏幕。经过调查,我在Logcat中看到以下内容
E/BufferQueueProducer: [unnamed-23827-0] queueBuffer: BufferQueue has been abandoned
E/MediaPlayer: error (1, -38)
E/MediaPlayer: error (1, -38)
E/MediaPlayer: error (1, -38)
E/MediaPlayer: error (1, -38)
E/BufferQueueProducer: [unnamed-23827-0] connect(P): BufferQueue has been abandoned
以下是我在简历中调用的代码
player.seekTo(mVideoSeekPosition);
player.start();
仅供参考:我一直试图将此答案应用于我的案例,但我不能:What can I do when the BufferQueue has been abandoned?
更新
我挣扎着独自一人,但我仍在崩溃。所以我发布了整个代码以求帮助
private void setupVideoPlayingSystem(View root) {
textureView = (TextureView) root.findViewById(R.id.textureView);
textureView.setSurfaceTextureListener(this);
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
Log.d(TAG, "onSurfaceTextureAvailable");
if (null == surface) {
Log.d(TAG, "new surface");
surface = new Surface(surfaceTexture);
mediaPlayer = new MediaPlayer();
mediaPlayer.setSurface(surface);
mediaPlayer.setLooping(false);
}
/*
outstandingVideoRequest is IOU for orentation change (verifed: onResume before onSurfaceTextureAvailable)
but for cold startup, must check mVideoUrl
*/
if (outstandingVideoRequest && null != mVideoUrl) {
outstandingVideoRequest = false;
playNewVideo(mVideoUrl);
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
Log.d(TAG, "onSurfaceTextureSizeChanged");
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
Log.d(TAG, "onSurfaceTextureDestroyed");
return false;//leave destruction for onDestroy
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
private void playNewVideo(String url) {
if (null == mediaPlayer || null == surface) {
Log.d(TAG, "playNewVideo not ready");
synchronized (outstandingVideoRequest) {
Log.d(TAG, "playNewVideo outstandingVideoRequest");
outstandingVideoRequest = true;
}
} else {
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(getContext(), Uri.parse(url));
mediaPlayer.setLooping(false);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer player) {
Log.d(TAG, "onPrepared changeMediaPlayerDatasource");
onReadyToPlay(player);
}
});
} catch (Exception e) {//IOException && IllegalStateException
Log.d(TAG, "textureview playNewVideo ERORR");
e.printStackTrace();
}
}
}
private void resumeVideoUponReturningFromAnotherActivity() {
if (null == mediaPlayer || null == surface) {
Log.d(TAG, "resumeVideoUponReturningFromAnotherActivity outstandingVideoRequest");
outstandingVideoRequest = true;
} else {
// playNewVideo(mVideoUrl);
Log.d(TAG, "resumeVideoUponReturningFromAnotherActivity go NOW");
mediaPlayer.setSurface(surface);
onReadyToPlay(mediaPlayer);
}
}
private void onReadyToPlay(MediaPlayer player) {
//play video
mProgressCircle.setVisibility(View.GONE);
showVideoOverlayChildren();
if (0 == mVideoSeekPosition) {
Log.d(TAG, "onReadyToPlay start");
player.start();
} else {
Log.d(TAG, "onReadyToPlay seek");
player.seekTo(mVideoSeekPosition);
player.start();
}
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Log.d(TAG, "postDelayed resumeVideo");
hideVideoOverlayChildren();
}
}, Constant.BEFORE_VIDEO_OVERLAY_DISAPPEAR);
}
private void destroyMediaPlayer() {
if (null != mediaPlayer) {//move to video todo
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
if (null != surface) {
surface.release();
surface = null;
}
}
private void pauseVideo() {
if (null != mediaPlayer) {
Log.d(TAG, "pause");
mediaPlayer.pause();
mVideoSeekPosition = mediaPlayer.getCurrentPosition();
}
}
private void stopVideo(){
if (null != mediaPlayer) {
Log.d(TAG, "stop video");
mediaPlayer.pause();
mVideoSeekPosition = mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
}
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume");
mLocalBroadcastManager.registerReceiver(mVideoSelectionReceiver, mVideoSelectedIntentFilter);
resumeVideoUponReturningFromAnotherActivity();
}
答案 0 :(得分:8)
我在活动之间切换时遇到了同样的问题,也有MediaPlayer(1971):错误(100,0)。通过在onSurfaceTextureDestroyed
中添加这些行来解决它 @Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
return true;
}
答案 1 :(得分:0)
您的代码中似乎存在错误: 在SurfaceTextureDestroyed()中,您没有重置surface或mediaPlayer。当恢复时,mediaPlayer和surface都不为null,所以在resumeVideoUponReturningFromAnotherActivity()中设置表面并调用start开始播放,但由于之前的SurfaceTextureDestroyed,表面已经变得无效。这就是你得错的原因。
要修复它,您应该在回调SurfaceTextureDestroyed中重置表面。恢复时,在回调SurfaceTextureAvailable中重建曲面,将其设置为mediaPlayer并调用start to play。代码是这样的:
public void onResume() {
if (mSurface == null) {
mResumeRequested = true;
return;
}
mMediaPlayer.start();
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mSurface = new Surface(surface);
if (mMediaPlayer != null) {
mMediaPlayer.setSurface(mSurface);
if (mResumeRequested) {
mMediaPlayer.start();
mResumeRequested = false;
}
}
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mSurface = null;
return false;
}
您根本不需要重置媒体播放器。如果重置,则必须重新实例化并重新缓冲,这会导致延迟,这会损害用户体验,因为不再需要延迟暂停/恢复。
答案 2 :(得分:0)
我发现setSurface(null)
很有用。
如果您使用TextureView显示内容,则在调用TextureView.SurfaceTextureListener
回调onSurfaceTextureDestroyed
时,您必须停止使用SurfaceTexture/new Surface(SurfaceTexture)
,camera2
绑定的MediaCodec
或MediaPlayer
。
喜欢这个
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mediaPlayer.setDisplayer(null);
return false;//do not return true if you reuse it.
}