Picture-in-picture for a video player on Android phones
On an Android phone, a video can shrink into a small floating window and keep playing while the viewer uses another app. It looks like a single feature, but it is really a handful of small decisions: what the activity declares, how the app enters the window, what the window shows, how each player engine feeds it, and what the screen does while it is that small.
Here is how Yonto Player's phone and tablet app makes each one, and why.
The activity declares support
Picture-in-picture is a mode of an activity, so the manifest is where it starts. A simplified outline of the flags that matter on the player's activity:
<!-- Simplified outline, not the real file. -->
<activity android:name=".MainActivity"
android:supportsPictureInPicture="true"
android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout|density|..."
android:launchMode="singleTask" />
supportsPictureInPicture is the declaration itself.
The configChanges list matters just as much here. Entering and leaving the window resizes the activity, and the same list covers rotation and folding. Because the activity handles those changes itself, a resize can be a relayout in Compose rather than a recreation of the activity.
singleTask keeps the app to one instance.
The app also asks the device at run time. If the device lacks the picture-in-picture system feature, the code that would enter the window returns nothing, and the player screen simply shows no button.
The way in
The viewer gets into the window with a Picture in picture button in the player's controls. The screen holds a small optional function for it: the platform layer supplies one that enters the window, or nothing where the device cannot, and the button appears only when there is one.
The parameters
The parameters are rebuilt whenever the picture's shape changes. This is a simplified sketch of what is set:
// Simplified sketch of the real code; names are generic.
val params = PictureInPictureParams.Builder()
.setAspectRatio(clampedAspect) // the video's shape, within what the system allows
.setSourceRectHint(pictureBounds) // where the picture sits now, for a smooth shrink
.apply {
if (drawOwnButtons) setActions(buttons)
if (sdk >= 31) setSeamlessResizeEnabled(true)
}
.build()
activity.setPictureInPictureParams(params) // kept in step while the player is on screen
// the button calls: activity.enterPictureInPictureMode(params)
Two details are worth spelling out.
The aspect ratio is clamped. The system accepts shapes only between roughly 2.39:1 and 1:2.39, so the code limits the video's own ratio to that range. A film wider than 2.39:1 is letterboxed inside a 2.39:1 window.
The source rectangle hint is where the picture currently sits on screen, letterboxed as it is drawn. With it, the system grows the window out of the picture rather than out of the whole screen, which makes the shrink look like one motion.
What the window shows
The window shows the picture and nothing else. The player's own controls, gestures and zoom have no place there, so the screen checks whether the activity is in the window and draws only the video, fitted whole whatever zoom the viewer had set. A spinner appears over it if the stream is still loading.
The window's controls are partly the system's. For the device's own player, the media session describes the title to the system, and which controls the system draws then depends on the device and on the actions the app exposes.
Where the app supplies them, it offers three remote actions:
- seek back
- play or pause
- seek forward
Each is a small pending intent to a receiver registered only while the player is composed. The receiver calls the same engine functions the on-screen controls call, using the step lengths the viewer set in the player settings.
One behaviour for two engines
The app has two players: the device's own (media3's ExoPlayer) and a second, software one for formats the first cannot decode. The window should behave the same under both, so the code asks the engine only for what it needs.
The picture's size comes from either one. ExoPlayer reports it through a listener, and the software player exposes its video size directly. Both feed the same aspect-ratio function.
The buttons differ for a good reason. The software player has no media session, so the system has nothing to draw its buttons from. For that engine, the app draws its own three buttons on every Android version. The parameters builder takes a systemDrawsButtons argument, and the platform sets it from whether the engine is the device's own.
Everything after that is shared: one entry function, one receiver, one window.
This is the design rule behind most of the phone player: the screen talks to an engine interface, and platform differences sit behind a small platform interface (here, "enter the window" and "am I in the window"). The shared UI code does not name Android.
When the window is small
Inside the window, the screen returns early. Once the picture is drawn, it skips the controls, the gesture layer, the panels and the overlays, because none of them can be used at that size. Panels that were open are closed on entry.
Two other panes have window versions of their own: if a title is still being prepared, a spinner stands in; if a title cannot be played, a short headline replaces the full message and the retry button, which a window has no room for.
The full-screen player's system-bar handling steps aside while in the window, since the window is the system's to shape.
When playback ends in the window
Series are where this shows up. When an episode ends and a next one is known, the app counts down and plays it, as it does in full screen. The countdown lives in the player state rather than in the pane that displays it, because the window has no pane. So the next episode can start inside the window.
With no next episode, the window keeps the last picture, and the full-screen choices are there when the viewer expands it.
What I would take from it
Keep the decision, the condition, and the platform call in different places. The screen knows whether there is a window to offer and draws the right thing in it. The platform knows how to enter it. The engines know their picture's size and whether the system can draw their buttons. Each of those is a small piece, and the window is what results when they agree.
Read more
- Video format support, which player plays what and where picture-in-picture is available
- Video formats Yonto Player plays
- Sources in Yonto Player
- Changelog
Comments
No comments yet. Start the discussion.