Android 8.0 allows Activiy to display in picture-in-picture mode. This is an enhancement of the multi-window mode, which is very useful in video applications. With this mode, you can open another app in the middle of a video call or watching a live broadcast without exiting the current video. More details will not be too tired, you go to read the official document on the line

Here is an example of this feature using the Agora SDK, without actually making any changes to it.

If you have any problems in reference and development, please visit the QUESTION and answer section of Agora to communicate with the engineers of Soundnet.

Prepare the environment

  • Android 8.0 or later

  • Agora SDK 1.14.0 or later

  • Android Studio 3.0 or above (not required)

How to realize picture-in-picture mode

The default app does not support picture-in-picture mode, so you need to configure the Activity where the video is located. The following in AndroidManifest. Add attribute resizeableActivity/supportsPictureInPicture XML and are set to true

android:resizeableActivity="true"
android:supportsPictureInPicture="true"
android:configChanges=
  "screenSize|smallestScreenSize|screenLayout|orientation"
Copy the code

In order to enter the picture in picture mode, Activty must use enterPictureInPictureMode (PictureInPictureParams params) method, is very simple, but to tell the system into picture in picture mode, We need to set some parameters for the layout of the Activity screen across the screen. Here we have a simple setting. The specific setting needs to be set dynamically according to the resolution of the screen. Please refer to the official document for more information.

PictureInPictureParams params = new PictureInPictureParams.Builder()
     .setAspectRatio(new Rational(10, 16))
     .build();
Copy the code

It is, of course, very easy to control things in the AcElasticity interface in the application, to hide native preview screens, to hide unwanted button information, and so on.

@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); }Copy the code

If you enter picture in Picture mode, the system will trigger the life cycle method onPause/onResume. If you enter picture in picture mode, you will do nothing and the video stream will continue. If you enter picture in picture mode, you will close the video stream and you will not see the video in background anyway.

Run a screenshot

Let’s get started

What are you waiting for? Now that you know this, add this feature to your app. Sample code is also provided for reference.

Author: Guo HaiCopy the code