這部分看了網(wǎng)上一些文章,說(shuō)的都很含糊,而且都是抄襲的哑诊。
總結(jié)下最近使用的感受和經(jīng)驗(yàn)撒璧。
UI components
官網(wǎng)demo說(shuō)的已經(jīng)很詳細(xì)透葛,但是很多細(xì)節(jié)google都沒(méi)處理,很多也沒(méi)處理好卿樱。如果不是為了字幕的加載僚害,和需求特殊,我可能不會(huì)選擇exo單獨(dú)使用繁调。
gradle 依賴(lài)
implementation 'com.google.android.exoplayer:exoplayer-ui:2.X.X'
重要的組件萨蚕,StyledPlayerControlView, StyledPlayerView, PlayerControlView and PlayerView.
樣式化的變體提供了很全靶草,但是更難定制。
StyledPlayerControlView and PlayerControlView
是用于控制回放的視圖岳遥。它們顯示標(biāo)準(zhǔn)播放控件奕翔,包括播放/暫停按鈕、快進(jìn)和快退按鈕以及搜索欄浩蓉。
PlayerControlView and PlayerView.
它們?cè)诓シ胚^(guò)程中顯示視頻派继、字幕和專(zhuān)輯藝術(shù),以及分別使用StyledPlayerControl視圖或PlayerControl視圖的播放控件妻往。
Player views
根據(jù)官網(wǎng)介紹
<com.google.android.exoplayer2.ui.StyledPlayerView
android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:show_buffering="when_playing"
app:show_shuffle_button="true"/>
xml 里面這么生命使用基本demo就可以互艾。但是如果自定義定制修改,需要重寫(xiě)StyledPlayerView讯泣,或者PlayerView .挺讓人頭大纫普,可能不是架構(gòu)功能,所以Google寫(xiě)的很死
Player control views
官網(wǎng)xml
<com.google.android.exoplayer2.ui.StyledPlayerControlView
android:id="@+id/player_control_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
很明顯好渠。Sample例子:StyledPlayerView 和 StyledPlayerControlView 是配套使用昨稼。PlayerView 和 PlayerControlView 配套
Customization
自定義,最讓人心煩拳锚,也是很苦惱的問(wèn)題假栓,功能很全但是改UI就很苦惱。我主要看重他的字幕功能霍掺。
如果想定制drawable和layout還是有很多坑的匾荆。(就像接入融云等一些聊天的UI)
官網(wǎng)修改UI簡(jiǎn)單做法
在自己項(xiàng)目中寫(xiě)一個(gè)一樣名字的xml, exo_player_control_view.xml
如:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton android:id="@id/exo_play"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#CC000000"
style="@style/ExoMediaButton.Play"/>
<ImageButton android:id="@id/exo_pause"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#CC000000"
style="@style/ExoMediaButton.Pause"/>
</FrameLayout>
或者
覆蓋布局文件是在整個(gè)應(yīng)用程序中更改布局的最佳解決方案杆烁,但如果只在單個(gè)位置需要自定義布局牙丽,該怎么辦?要實(shí)現(xiàn)這一點(diǎn)兔魂,首先定義一個(gè)布局文件烤芦,就像覆蓋一個(gè)默認(rèn)布局一樣,但這次給它一個(gè)不同的文件名析校,例如custom_controls.xml构罗。其次,使用一個(gè)屬性指示在膨脹視圖時(shí)應(yīng)使用此布局智玻。例如遂唧,在使用PlayerView時(shí),可以使用controller_layout_id屬性指定用于提供播放控件的布局
<com.google.android.exoplayer2.ui.PlayerView android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:controller_layout_id="@layout/custom_controls"/>
這樣也只能修改簡(jiǎn)單的按鈕吊奢,和他預(yù)定義定死的按鈕蠢箩。
吐槽
Google定死了id,你必須按照他的來(lái),否則就像我一樣全部重寫(xiě)谬泌,或者部分重寫(xiě)
定制需要吧values.xml, 拿出來(lái)放到自己的項(xiàng)目滔韵。styles.xml 也是覆寫(xiě)PlayerView相關(guān)就把代碼粘貼出來(lái)改。
關(guān)于控制掌实,你必須通讀UI相關(guān)源代碼陪蜻。而且全屏和旋轉(zhuǎn)都需要自己控制。
設(shè)計(jì)師贱鼻,必須有播放器和技術(shù)相關(guān)經(jīng)驗(yàn)宴卖,不然程序累死找bug定制UI
不建議用,特殊情況可以邻悬,根據(jù)需求定制吧症昏。
解耦太難。