這里以 Agora SDK 為例來給大家展示下該特性,實際上不用 Agora SDK 做任何修改。
準備環(huán)境
- Android 8.0 或以上版本手機
- Agora SDK 1.14.0 或 以上 版本
- Android Studio 3.0 或以上版本(非必需)
如何實現(xiàn)畫中畫模式
默認應(yīng)用是不支持畫中畫模式的,需要給視頻所在的 Activity 做些配置,如下在 AnroidManifest.xml加上屬性 resizeableActivity/supportsPictureInPicture 并均設(shè)置為 true
- android:resizeableActivity="true"
- android:supportsPictureInPicture="true"
- android:configChanges=
- "screenSize|smallestScreenSize|screenLayout|orientation"
為了進入畫中畫模式,Activty 必需要用 enterPictureInPictureMode(PictureInPictureParams params)方法,非常的簡單,但是為了告訴系統(tǒng)進入畫中畫模式之后,Activity 界面在整個屏幕當中的布局,我們需要設(shè)置一些參數(shù)。我們這里簡單設(shè)置下,具體在使用的時候需要根據(jù)屏幕的分辨率動態(tài)取設(shè)置,更多信息參考 官方文檔。
PictureInPictureParams params = new PictureInPictureParams.Builder()
- setAspectRatio(new Rational(10, 16))
- build();
當然需要在程序當中控制 Acticity 界面當中的內(nèi)容,比如我們可以隱藏自己本地的預(yù)覽畫面,隱藏不需要的按鈕信息等等,這個實現(xiàn)也非常簡單。
@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
FrameLayout container = findViewById(R.id.local_video_view_container);
SurfaceView surfaceView = (SurfaceView) container.getChildAt(0);
surfaceView.setZOrderMediaOverlay(!isInPictureInPictureMode);
surfaceView.setVisibility(isInPictureInPictureMode ? View.GONE : View.VISIBLE);
container.setVisibility(isInPictureInPictureMode ? View.GONE : View.VISIBLE);
}
另外值得一說的是,進入畫中畫模式,系統(tǒng)會觸發(fā)生命周期的方法 onPause/onResume 方法,我們需要根據(jù)需要適當?shù)淖鲂┎僮鳎热缡钱嬛挟嬆J降脑,就不做任何操作,音視頻流繼續(xù),否則的話,就關(guān)閉視頻流,反正在后臺也看不見視頻。
運行截圖

開始行動吧
還等什么,了解了這些之后,快給自己的應(yīng)用加上這個功能吧。我們也提供了 示例代碼供參考。
Agora SDK下載鏈接:https://document.agora.io/cn/2.0/sdk/
Android官方文檔:https://developer.android.com/guide/topics/ui/picture-in-picture.html
示例代碼:https://github.com/AgoraIO/Agora-Picture-in-Picture-Android
課程預(yù)告:從0開發(fā)Android視頻通話
時間:12月13日(周三)16:00-17:00
內(nèi)容:
- 通信和直播的基本概念
- Agora.SDK 2.0重大更新介紹
- Agora Android SDK API調(diào)用邏輯
- 開發(fā)環(huán)境準備
- 快速完成一個Agora android demo,功能包括:創(chuàng)建頻道、加入頻道、創(chuàng)建視頻通話、離開頻道、開關(guān)攝像頭、靜音、畫中畫。
- 如何測試。視頻通話需要進行哪些測試,如何進行簡單的測試
- Q&A