解决 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;
      
    }
  }
相关推荐
一枚小小程序员哈30 分钟前
基于Android的随身小管家APP的设计与实现/基于SSM框架的财务管理系统/android Studio/java/原生开发
android·ide·android studio
stevenzqzq1 小时前
android data 文件夹作用
android
2501_915918411 小时前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
auxor5 小时前
Android 开机动画音频播放优化方案
android
whysqwhw6 小时前
安卓实现屏幕共享
android
深盾科技6 小时前
Kotlin Data Classes 快速上手
android·开发语言·kotlin
一条上岸小咸鱼6 小时前
Kotlin 基本数据类型(五):Array
android·前端·kotlin
whysqwhw7 小时前
Room&Paging
android
whysqwhw7 小时前
RecyclerView超长列表优化
android
Tiger_Hu7 小时前
Android系统日历探索
android