Implement Multi-Window Picture-in-Picture on a Video Player


Objective

Learn how to implement Multi-Window Picture-in-Picture (PiP) feature on a video player app for a seamless viewing experience on Foldable devices.

Overview

Android allows activities of apps to launch in a small window called picture-in-picture (PiP) since Android 8.0. It is a type of multi-window mode primarily used for video playback. PiP allows the user to continue watching in a small window pinned to a corner of the screen while browsing content on the main screen or navigating between apps.

Set up your environment

You will need the following:

  • Java SE Development Kit (JDK) 8 or later

  • Android Studio (latest version recommended)

  • Samsung Galaxy Fold, Z Fold2, Z Fold3, or Z Fold4

  • Remote Test Lab (if physical device is not available)

    Requirements:

    • Samsung account
    • Java Runtime Environment (JRE) 7 or later with Java Web Start
    • Internet environment where port 2600 is available

Sample Code

Here is a sample code for you to start coding in this Code Lab. Download it and start your learning experience!

PiP Video Player Sample Code
(13.30 MB)

Start your project

After downloading the sample project files, follow the steps below to open your project:

  1. In Android Studio, click Open.

  1. Locate the downloaded Android Project (VideoPlayer_Challenge) from the directory and click OK.

Configure Android manifest to support PiP

You can implement picture-in-picture in your app by configuring the manifest file and calling the enterPictureInPictureMode method. Moreover, you need to consider that the multi-window and app continuity features work correctly in PiP mode.

  1. To prevent the activity from restarting, handle the layout configuration change for MainActivity in the AndroidManifest.xml file. This will ensure that it will not restart when there are layout changes from PiP mode to full screen and vice versa.
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
  1. Picture-in-picture is not supported by default in Android apps. Set android:supportsPictureInPicture to true in the manifest file.
android:supportsPictureInPicture="true"

Switch your video player to PiP mode

You want to trigger PiP mode in your video player app when the user taps the Home button.

To implement PiP mode, go to java > com.xyz.codelab and make the following changes to the MainActivity.kt file:

override fun onUserLeaveHint() {
   enterPipMode()
}

Call enterPictureInPictureMode method

In enterPipMode(), you need to call enterPictureInPictureMode(pictureInPictureParams) method with params set in the previous lines.

val pictureInPictureParams = PictureInPictureParams.Builder().setAspectRatio(rational).build()
enterPictureInPictureMode(pictureInPictureParams)

Run the app

After building the APK, you can now run the video player app, enhanced with a PiP mode. Test it by playing a video while browsing the main screen or navigating between apps in multi-window mode. If you don’t have any physical device, you can also test it on a Remote Test Lab device.


You're done!

Congratulations! You have successfully achieved the goal of this Code Lab. Now, you can implement multi-window picture-in-picture on a video player app for foldable devices by yourself! If you're having trouble, you may download this file:

PiP Video Player Complete Code
(13.19 MB)

To learn more about developing apps for Galaxy Foldable devices, visit:
developer.samsung.com/galaxy-z