解决 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;
      
    }
  }
相关推荐
机智的张尼玛14 小时前
我基于 Kotlin Multiplatform 实现了一套跨平台的弱网离线同步引擎
android·开源·kotlin
安卓修改大师17 小时前
安卓修改大师反编译引擎Apktool深度解析:版本选择、参数说明与实战技巧
android
error:(17 小时前
【系统与实战双精通】VS Code 调试 ROS2 Python 节点与 Launch 系统指南
android·java·python
黄林晴19 小时前
Kuikly 是什么?和KMP有什么关系?
android·前端
帅次20 小时前
Android 高级工程师面试:Kotlin 语法基础 近1年高频追问 22 题
android·面试·kotlin·扩展函数·空安全
想你依然心痛20 小时前
嵌入式日志系统:分级日志、环形缓冲区与远程输出——运行时调试、非侵入
android·前端·javascript
用户693717500138421 小时前
AI Agent 里的 Loop 到底是什么?
android·前端·后端
海天鹰1 天前
安卓分割空字符串数组长度为1
android
Coffeeee1 天前
从 Android 15 到 Android 17,谷歌猝不及防的音频改动
android·前端·google
用户69371750013841 天前
AI 领域的 Harness,到底是什么意思?
android·前端·后端