Skip to content

Commit 0ce6be1

Browse files
Update PiP snippets with new aspect ratio best practice (#257)
* Update PictureInPictureSnippets.kt * Update PictureInPictureSnippets.kt * Apply Spotless * Update PictureInPictureSnippets.kt * Update PictureInPictureSnippets.kt * Update PictureInPictureSnippets.kt --------- Co-authored-by: MagicalMeghan <[email protected]>
1 parent 30f5ef7 commit 0ce6be1

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/pictureinpicture/PictureInPictureSnippets.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import androidx.core.content.ContextCompat
4747
import androidx.core.graphics.toRect
4848
import androidx.core.util.Consumer
4949
import androidx.media3.common.Player
50+
import androidx.media3.common.VideoSize
5051
import androidx.media3.exoplayer.ExoPlayer
5152

5253
var shouldEnterPipMode by mutableStateOf(false)
@@ -237,6 +238,7 @@ fun PiPBuilderSetSourceRect(
237238

238239
@Composable
239240
fun PiPBuilderSetAspectRatio(
241+
player: Player?,
240242
shouldEnterPipMode: Boolean,
241243
modifier: Modifier = Modifier,
242244
) {
@@ -246,12 +248,11 @@ fun PiPBuilderSetAspectRatio(
246248

247249
val pipModifier = modifier.onGloballyPositioned { layoutCoordinates ->
248250
val builder = PictureInPictureParams.Builder()
249-
250-
if (shouldEnterPipMode) {
251+
if (shouldEnterPipMode && player != null && player.videoSize != VideoSize.UNKNOWN) {
251252
val sourceRect = layoutCoordinates.boundsInWindow().toAndroidRectF().toRect()
252253
builder.setSourceRectHint(sourceRect)
253254
builder.setAspectRatio(
254-
Rational(sourceRect.width(), sourceRect.height())
255+
Rational(player.videoSize.width, player.videoSize.height)
255256
)
256257
}
257258

@@ -312,6 +313,7 @@ fun listOfRemoteActions(): List<RemoteAction> {
312313

313314
@Composable
314315
fun PiPBuilderAddRemoteActions(
316+
player: Player?,
315317
shouldEnterPipMode: Boolean,
316318
modifier: Modifier = Modifier,
317319
) {
@@ -325,11 +327,11 @@ fun PiPBuilderAddRemoteActions(
325327
listOfRemoteActions()
326328
)
327329

328-
if (shouldEnterPipMode) {
330+
if (shouldEnterPipMode && player != null && player.videoSize != VideoSize.UNKNOWN) {
329331
val sourceRect = layoutCoordinates.boundsInWindow().toAndroidRectF().toRect()
330332
builder.setSourceRectHint(sourceRect)
331333
builder.setAspectRatio(
332-
Rational(sourceRect.width(), sourceRect.height())
334+
Rational(player.videoSize.width, player.videoSize.height)
333335
)
334336
}
335337

0 commit comments

Comments
 (0)