解决 GSYVideoPlayer 连续切换视频 出现黑屏只有声音没有画面的问题

问题描述

如果这个播放器用来recycleView中,实现类似抖音的效果,会发现刷了一段时间,会出现只有声音没有画面的情况。这个时候是因为surface没有创建(具体原因未知)。GSYTextureView 中的onSurfaceTextureAvailable方法没有回调。

其他人也遇到过
GSYTextureRenderView onSurfaceAvailable 不调用(已添加硬件加速) #1740

解决方案

如果发现surface没有创建成功就手动调用。继承StandardGSYVideoPlayer 重写以下四个方法

java 复制代码
public class TRSVideoPlayer extends StandardGSYVideoPlayer {

    boolean isSurfaceAvailable;
    @Override
    protected void setProgressAndTime(int progress, int secProgress, int currentTime, int totalTime, boolean forceChange) {
        super.setProgressAndTime(progress, secProgress, currentTime, totalTime, forceChange);
        //以下代码要解决的问题是,如果这个播放器用来recycleView中,实现类似抖音的效果
        //会发现刷了一段时间,会出现只有声音没有画面的情况。这个时候是因为surface没有创建(具体原因未知)
        //通过调用requestLayout可以创建surface。但是只在当前控件上调用requestLayout不起作用
        //需要一直往上调用。
        if (getCurrentState() == CURRENT_STATE_PLAYING || getCurrentState() == CURRENT_STATE_PLAYING_BUFFERING_START) {
            if (!isSurfaceAvailable) {
                Debuger.printfLog("VideoView 播放器缓冲中 isAddRenderView " + isSurfaceAvailable + " 当前播放器状态 " + getCurrentState());
                try {
                  requestLayoutLoop(this);
                } catch (Exception | OutOfMemoryError e) {
                    onCompletion();
                }

                return;
            }
        }
    }
    //一直循环向上调用。
    private void requestLayoutLoop(Object obj){
        if(obj==null){
            return;
        }
        if(!(obj instanceof View)){
            return;
        }
        View view= (View) obj;

        view.requestLayout();
        requestLayoutLoop(view.getParent());
    }

    @Override
    protected void addTextureView() {
        super.addTextureView();
  		//每次添加TextrueView就重置状态
        isSurfaceAvailable = false;
    }

    @Override
    public void onSurfaceAvailable(Surface surface) {
        super.onSurfaceAvailable(surface);
        isSurfaceAvailable = true;
      
    }
  }
相关推荐
故渊at8 小时前
第二板块:Android 四大组件标准化学理 | 第六篇:四大组件架构总论与 Manifest 规范
android·架构·zygote·manifest·四大组件
Jinkxs9 小时前
Python基础 - 文件的写入操作 write与writelines方法
android·服务器·python
jason.zeng@15022079 小时前
(第二讲)Android开发取摄像头流的基础(ImageAnalysis)
android
敲代码的瓦龙10 小时前
操作系统?Android与Linux!!!
android·linux·运维
愚公搬代码10 小时前
【愚公系列】《移动端AI应用开发》017-Android端应用开发(网络通信与API集成)
android·人工智能
say_fall10 小时前
可编程中断控制器8259A工作方式超详细解析
android·开发语言·学习·硬件架构·硬件工程
甜瓜看代码12 小时前
SystemUI 启动与组成机制
android·源码·源码阅读
黄林晴13 小时前
Kotlin 2.4.0 正式稳定!Android 升级、Compose、KMP 全变化详解
android·kotlin
恋猫de小郭14 小时前
Android 官方给 Compose 搞了个不需要 UI 环境的 Composable
android·前端·flutter
珊瑚里的鱼15 小时前
C++的强制类型转换
android·开发语言·c++