BufferQueue has been abandoned: When playing video with TextureView

不想你离开。 提交于 2019-11-30 05:09:02

I had the same problem when switched between activities and also had MediaPlayer(1971): Error (100,0). Solved it by adding these lines inside onSurfaceTextureDestroyed

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        return true;
    }

It seems there is a bug in your code: in SurfaceTextureDestroyed() you did not reset surface or mediaPlayer. When resume, neither mediaPlayer nor surface is null, so in resumeVideoUponReturningFromAnotherActivity() set the surface and call start to play, but surface already become invalid because of previous SurfaceTextureDestroyed. That's why you get error.

To fix it, you should reset surface in callback SurfaceTextureDestroyed. When resume, rebuild surface in callback SurfaceTextureAvailable, set it to mediaPlayer and call start to play. The codes go like this:

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;
}

And you do not have to reset media player at all. If you reset, you have to re-instantiate it and re-buffering, which causes delay, this harms user experience because no delaying pause/resume is more desired.

miao

I find setSurface(null) is useful.

If you use a TextureView to display something, when TextureView.SurfaceTextureListener callback onSurfaceTextureDestroyed was called, you must stop use SurfaceTexture/new Surface(SurfaceTexture) binded by camera2, MediaCodec or MediaPlayer.

Like this

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
   mediaPlayer.setDisplayer(null);
   return false;//do not return true if you reuse it.
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!