美文网首页简化开发
GSYVideoPlayer视频显示过程笔记

GSYVideoPlayer视频显示过程笔记

作者: 紫鹰 | 来源:发表于2022-06-22 14:41 被阅读0次

    项目中使用了GSYVideoPlayer,查看他们的demo,项目地址 https://github.com/CarGuo/GSYVideoPlayer

    发现了一件让猴子有点疑惑的事

    他们的demo中的自定义视频播放器控件 SampleCoverVideo,设置了R.layout.video_layout_cover,然而,布局文件里面压根没有视频播放器相关的控件。布局文件如下

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black">
    
    <FrameLayout
        android:id="@+id/surface_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
    
    </FrameLayout>
    
    <RelativeLayout
        android:id="@+id/thumb"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:background="#000000"
        android:scaleType="fitCenter">
    
        <ImageView
            android:id="@+id/thumbImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop" />
    
    </RelativeLayout>
    
    <LinearLayout
        android:id="@+id/layout_bottom"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:background="#99000000"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:visibility="invisible">
    
        <TextView
            android:id="@+id/current"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:text="00:00"
            android:textColor="#ffffff" />
    
        <SeekBar
            android:id="@+id/progress"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1.0"
            android:background="@null"
            android:max="100"
            android:maxHeight="4dp"
            android:minHeight="4dp"
            android:paddingTop="8dp"
            android:paddingBottom="8dp"
            android:progressDrawable="@drawable/video_seek_progress"
            android:thumb="@drawable/video_seek_thumb" />
    
        <TextView
            android:id="@+id/total"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp"
            android:text="00:00"
            android:textColor="#ffffff" />
    
        <ImageView
            android:id="@+id/fullscreen"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:paddingRight="16dp"
            android:scaleType="center"
            android:src="@drawable/video_enlarge" />
    </LinearLayout>
    
    <ProgressBar
        android:id="@+id/bottom_progressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="1.5dp"
        android:layout_alignParentBottom="true"
        android:max="100"
        android:progressDrawable="@drawable/video_progress" />
    
    <ImageView
        android:id="@+id/back_tiny"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginLeft="6dp"
        android:layout_marginTop="6dp"
        android:visibility="gone" />
    
    <LinearLayout
        android:id="@+id/layout_top"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@drawable/video_title_bg"
        android:gravity="center_vertical">
    
        <ImageView
            android:id="@+id/back"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:paddingLeft="10dp"
            android:scaleType="centerInside"
            android:src="@drawable/video_back" />
    
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:textColor="@android:color/white"
            android:textSize="18sp" />
    </LinearLayout>
    
    <moe.codeest.enviews.ENDownloadView
        android:id="@+id/loading"
        android:layout_width="28dp"
        android:layout_height="28dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:visibility="invisible" />
    
    <moe.codeest.enviews.ENPlayView
        android:id="@+id/start"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical" />
    
    
    <ImageView
        android:id="@+id/small_close"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:scaleType="centerInside"
        android:src="@drawable/video_small_close"
        android:visibility="gone" />
    
    <ImageView
        android:id="@+id/lock_screen"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="50dp"
        android:scaleType="centerInside"
        android:src="@drawable/unlock"
        android:visibility="gone" />
    

    </RelativeLayout>

    接下来,就探究一下,视频是如何添加到view上面去的

    仔细看demo的布局文件,唯一有可能和视频播放器相关的就是id为surface_container的一个FrameLayout,去代码中找一下,最终在自定义的顶层父类GSYTextureRenderView中找到protected ViewGroup mTextureViewContainer;

    与mTextureViewContainer相关方法为

     protected void addTextureView() {
        mTextureView = new GSYRenderView();
        mTextureView.addView(getContext(), mTextureViewContainer, mRotate, this, this, 
        mEffectFilter, mMatrixGL, mRenderer, mMode);
    }
    

    该方法在视频初始化,恢复播放等地方调用

    在addTextureView方法中,mTextureViewContainer被当成参数传递给了GSYRenderView中的addView方法

    看一下GSYRenderView中的addView方法

    public void addView(final Context context, final ViewGroup textureViewContainer, final int rotate,
                    final IGSYSurfaceListener gsySurfaceListener,
                    final MeasureHelper.MeasureFormVideoParamsListener videoParamsListener,
                    final GSYVideoGLView.ShaderInterface effect, final float[] transform,
                    final GSYVideoGLViewBaseRender customRender, int mode) {
        if (GSYVideoType.getRenderType() == GSYVideoType.SUFRACE) {
            mShowView = GSYSurfaceView.addSurfaceView(context, textureViewContainer, 
    rotate, gsySurfaceListener, videoParamsListener);
        } else if (GSYVideoType.getRenderType() == GSYVideoType.GLSURFACE) {
            mShowView = GSYVideoGLView.addGLView(context, textureViewContainer, rotate, gsySurfaceListener, videoParamsListener, effect, transform, customRender, mode);
        } else {
            mShowView = GSYTextureView.addTextureView(context, textureViewContainer, rotate, gsySurfaceListener, videoParamsListener);
        }
    }
    

    这里会根据GSYVideoType.getRenderType()的不同类型,将我们的mTextureViewContainer传递给三个不同的SurfaceView的子类中,以GSYVideoGLView的addGLView为例看一下

    public static GSYVideoGLView addGLView(final Context context, final ViewGroup textureViewContainer, final int rotate,
                                       final IGSYSurfaceListener gsySurfaceListener,
                                       final MeasureHelper.MeasureFormVideoParamsListener videoParamsListener,
                                       final GSYVideoGLView.ShaderInterface effect, final float[] transform,
                                       final GSYVideoGLViewBaseRender customRender, final int renderMode) {
        if (textureViewContainer.getChildCount() > 0) {
            textureViewContainer.removeAllViews();
        }
        final GSYVideoGLView gsyVideoGLView = new GSYVideoGLView(context);
        if (customRender != null) {
            gsyVideoGLView.setCustomRenderer(customRender);
        }
        gsyVideoGLView.setEffect(effect);
        gsyVideoGLView.setVideoParamsListener(videoParamsListener);
        gsyVideoGLView.setRenderMode(renderMode);
        gsyVideoGLView.setIGSYSurfaceListener(gsySurfaceListener);
        gsyVideoGLView.setRotation(rotate);
        gsyVideoGLView.initRender();
        gsyVideoGLView.setGSYVideoGLRenderErrorListener(new GSYVideoGLRenderErrorListener() {
            @Override
            public void onError(GSYVideoGLViewBaseRender render, String Error, int code, boolean byChangedRenderError) {
                if (byChangedRenderError)
                    addGLView(context,
                        textureViewContainer,
                        rotate,
                        gsySurfaceListener,
                        videoParamsListener,
                        render.getEffect(),
                        render.getMVPMatrix(),
                        render, renderMode);
    
            }
        });
        if (transform != null && transform.length == 16) {
            gsyVideoGLView.setMVPMatrix(transform);
        }
        GSYRenderView.addToParent(textureViewContainer, gsyVideoGLView);
        return gsyVideoGLView;
    }
    

    这个方法新建了一个GSYVideoGLView对象

    最终调用了 GSYRenderView.addToParent将textureViewContainer和新创建的GSYVideoGLView对象传递了进去。看一下addToParent方法

    public static void addToParent(ViewGroup textureViewContainer, View render) {
        int params = getTextureParams();
        if (textureViewContainer instanceof RelativeLayout) {
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(params, params);
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
            textureViewContainer.addView(render, layoutParams);
        } else if (textureViewContainer instanceof FrameLayout) {
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(params, params);
            layoutParams.gravity = Gravity.CENTER;
            textureViewContainer.addView(render, layoutParams);
        }
    }
    

    绕了一大圈(其实是一种管理复杂性的策略),将render(也就是新创建的GSYVideoGLView对象,SurfaceView的子类)添加到了我们的textureViewContainer中。

    看到了这里,明白了SurfaceView怎么添加到了布局中。那么新的问题出现了,怎么将SurfaceView设置给MediaPlayer的?那就继续追踪SurfaceView去那里了。

    继续看代码GSYVideoGLView的init方法

    private void init(Context context) {
        mContext = context;
        setEGLContextClientVersion(2);
        mRenderer = new GSYVideoGLViewSimpleRender();
        measureHelper = new MeasureHelper(this, this);
        mRenderer.setSurfaceView(GSYVideoGLView.this);
    }
    

    SurfaceView通过setSurfaceView方法传递给了GSYVideoGLViewSimpleRender在GSYVideoGLViewSimpleRender的onSurfaceCreated方法中

    @Override
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
    
        ...
    
        sendSurfaceForPlayer(surface);
    }
    

    以上方法最终调用了

    public void sendSurfaceForPlayer(final Surface surface) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (mGSYSurfaceListener != null) {
                    mGSYSurfaceListener.onSurfaceAvailable(surface);
                }
            }
        });
    }
    

    通过接口,传递给了自定义控件GSYVideoGLView中

    public void onSurfaceAvailable(Surface surface) {
        if (mIGSYSurfaceListener != null) {
            mIGSYSurfaceListener.onSurfaceAvailable(surface);
        }
    }
    

    这里也只是做了一个转发,发给了IGSYSurfaceListener的实现GSYTextureRenderView

    public void onSurfaceAvailable(Surface surface) {
        pauseLogic(surface, (mTextureView != null && mTextureView.getShowView() instanceof TextureView));
    }
    

    pauseLogic方法为

    protected void pauseLogic(Surface surface, boolean pauseLogic) {
        mSurface = surface;
        if (pauseLogic)
            //显示暂停切换显示的图片
            showPauseCover();
        setDisplay(mSurface);
    }
    

    最终调用了GSYVideoView的

    protected void setDisplay(Surface surface) {
        getGSYVideoManager().setDisplay(surface);
    }
    

    进入了GSYVideoBaseManager的

    public void setDisplay(Surface holder) {
        Message msg = new Message();
        msg.what = HANDLER_SETDISPLAY;
        msg.obj = holder;
        showDisplay(msg);
    }
    

    showDisplay方法

    private void showDisplay(Message msg) {
        if (playerManager != null) {
            playerManager.showDisplay(msg);
        }
    }
    

    以SystemPlayerManager为例看看

    public void showDisplay(Message msg) {
        if (msg.obj == null && mediaPlayer != null && !release) {
            mediaPlayer.setSurface(null);
        } else if (msg.obj != null) {
            Surface holder = (Surface) msg.obj;
            surface = holder;
            if (mediaPlayer != null && holder.isValid() && !release) {
                mediaPlayer.setSurface(holder);
            }
            if (!isPlaying) {
                pause();
            }
        }
    }
    

    终于看到了mediaPlayer.setSurface(holder)

    到了这里,总算是明白了MediaPlayer如何和页面关联起来的了。

    相关文章

      网友评论

        本文标题:GSYVideoPlayer视频显示过程笔记

        本文链接:https://www.haomeiwen.com/subject/fuhuvrtx.html