每当我暂停我的活动(实际上是片段)去另一个应用程序时,在恢复时尝试恢复视频播放,但它不会播放:我得到一个空屏幕。调查后,我在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();
FYI:我一直在尝试将这个答案应用于我的情况,但是我无法做到:当缓冲区队列被放弃时我该怎么办?
更新
我试图自己解决,并且仍然崩溃。因此,我发布完整的代码以寻求帮助。
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();
}
mediaPlayer.setSurface(surface);
,但仍然无法正常工作。此外,我还尝试了其他一些方法。 - learner