GSYVideoPlayer視頻顯示過程筆記

項(xiàng)目中使用了GSYVideoPlayer袖扛,查看他們的demo,項(xiàng)目地址 https://github.com/CarGuo/GSYVideoPlayer

發(fā)現(xiàn)了一件讓猴子有點(diǎn)疑惑的事

他們的demo中的自定義視頻播放器控件 SampleCoverVideo馍忽,設(shè)置了R.layout.video_layout_cover触菜,然而,布局文件里面壓根沒有視頻播放器相關(guān)的控件镰吵。布局文件如下

<?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上面去的

仔細(xì)看demo的布局文件,唯一有可能和視頻播放器相關(guān)的就是id為surface_container的一個(gè)FrameLayout,去代碼中找一下茵瘾,最終在自定義的頂層父類GSYTextureRenderView中找到protected ViewGroup mTextureViewContainer;

與mTextureViewContainer相關(guān)方法為

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

該方法在視頻初始化礼华,恢復(fù)播放等地方調(diào)用

在addTextureView方法中,mTextureViewContainer被當(dāng)成參數(shù)傳遞給了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);
    }
}

這里會根據(jù)GSYVideoType.getRenderType()的不同類型拗秘,將我們的mTextureViewContainer傳遞給三個(gè)不同的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;
}

這個(gè)方法新建了一個(gè)GSYVideoGLView對象

最終調(diào)用了 GSYRenderView.addToParent將textureViewContainer和新創(chuàng)建的GSYVideoGLView對象傳遞了進(jìn)去〉裰迹看一下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);
    }
}

繞了一大圈(其實(shí)是一種管理復(fù)雜性的策略)扮匠,將render(也就是新創(chuàng)建的GSYVideoGLView對象,SurfaceView的子類)添加到了我們的textureViewContainer中凡涩。

看到了這里棒搜,明白了SurfaceView怎么添加到了布局中。那么新的問題出現(xiàn)了活箕,怎么將SurfaceView設(shè)置給MediaPlayer的力麸?那就繼續(xù)追蹤SurfaceView去那里了。

繼續(xù)看代碼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);
}

以上方法最終調(diào)用了

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);
    }
}

這里也只是做了一個(gè)轉(zhuǎn)發(fā)末盔,發(fā)給了IGSYSurfaceListener的實(shí)現(xiàn)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);
}

最終調(diào)用了GSYVideoView的

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

進(jìn)入了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如何和頁面關(guān)聯(lián)起來的了座慰。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市翠拣,隨后出現(xiàn)的幾起案子版仔,更是在濱河造成了極大的恐慌,老刑警劉巖误墓,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蛮粮,死亡現(xiàn)場離奇詭異,居然都是意外死亡谜慌,警方通過查閱死者的電腦和手機(jī)然想,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來欣范,“玉大人变泄,你說我怎么就攤上這事∧涨恚” “怎么了妨蛹?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長晴竞。 經(jīng)常有香客問我蛙卤,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任颤难,我火速辦了婚禮神年,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘行嗤。我一直安慰自己已日,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布昂验。 她就那樣靜靜地躺著捂敌,像睡著了一般。 火紅的嫁衣襯著肌膚如雪既琴。 梳的紋絲不亂的頭發(fā)上占婉,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天,我揣著相機(jī)與錄音甫恩,去河邊找鬼逆济。 笑死,一個(gè)胖子當(dāng)著我的面吹牛磺箕,可吹牛的內(nèi)容都是我干的奖慌。 我是一名探鬼主播,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼松靡,長吁一口氣:“原來是場噩夢啊……” “哼简僧!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起雕欺,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤岛马,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后屠列,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體啦逆,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年笛洛,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了夏志。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,852評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡苛让,死狀恐怖沟蔑,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蝌诡,我是刑警寧澤溉贿,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站浦旱,受9級特大地震影響宇色,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一宣蠕、第九天 我趴在偏房一處隱蔽的房頂上張望例隆。 院中可真熱鬧,春花似錦抢蚀、人聲如沸镀层。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽唱逢。三九已至,卻和暖如春屋休,著一層夾襖步出監(jiān)牢的瞬間坞古,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工劫樟, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留痪枫,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓叠艳,卻偏偏與公主長得像奶陈,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子附较,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容