解决 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;
      
    }
  }
相关推荐
墨狂之逸才12 小时前
React Native 移动项目目录导致的 Android 编译失败问题及解决方案
android·react native
feng一样的男子12 小时前
住在手机里的“小龙虾” (OpenClaw):接入本地模型,解决记忆“装死”顽疾
android·ai·智能手机·openclaw
hongtianzai12 小时前
MySQL中between and的基本用法
android·数据库·mysql
Zender Han13 小时前
Flutter Bloc / Cubit 最新详解与实战指南(2026版)
android·flutter·ios
sun00770014 小时前
pthread_once
android
阿拉斯攀登14 小时前
第 20 篇 RK 平台 NPU / 硬件编解码驱动适配与安卓调用
android·驱动开发·瑞芯微·rk安卓驱动
Volunteer Technology15 小时前
mysql面试场景题(二)
android·mysql·面试
代码s贝多芬的音符16 小时前
Android NV21 转 YUV 系列格式
android·开发语言·python
匆忙拥挤repeat16 小时前
Android Compose 《编程思想》解读
android
进击的cc16 小时前
Activity 生命周期是如何被调度的?(从源码到实战全链路拆解)
android