Skip to content

Commit

Permalink
Update PiP snippets with new aspect ratio best practice (#257)
Browse files Browse the repository at this point in the history
* Update PictureInPictureSnippets.kt

* Update PictureInPictureSnippets.kt

* Apply Spotless

* Update PictureInPictureSnippets.kt

* Update PictureInPictureSnippets.kt

* Update PictureInPictureSnippets.kt

---------

Co-authored-by: MagicalMeghan <[email protected]>
  • Loading branch information
MagicalMeghan and MagicalMeghan authored May 8, 2024
1 parent 30f5ef7 commit 0ce6be1
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import androidx.core.content.ContextCompat
import androidx.core.graphics.toRect
import androidx.core.util.Consumer
import androidx.media3.common.Player
import androidx.media3.common.VideoSize
import androidx.media3.exoplayer.ExoPlayer

var shouldEnterPipMode by mutableStateOf(false)
Expand Down Expand Up @@ -237,6 +238,7 @@ fun PiPBuilderSetSourceRect(

@Composable
fun PiPBuilderSetAspectRatio(
player: Player?,
shouldEnterPipMode: Boolean,
modifier: Modifier = Modifier,
) {
Expand All @@ -246,12 +248,11 @@ fun PiPBuilderSetAspectRatio(

val pipModifier = modifier.onGloballyPositioned { layoutCoordinates ->
val builder = PictureInPictureParams.Builder()

if (shouldEnterPipMode) {
if (shouldEnterPipMode && player != null && player.videoSize != VideoSize.UNKNOWN) {
val sourceRect = layoutCoordinates.boundsInWindow().toAndroidRectF().toRect()
builder.setSourceRectHint(sourceRect)
builder.setAspectRatio(
Rational(sourceRect.width(), sourceRect.height())
Rational(player.videoSize.width, player.videoSize.height)
)
}

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

@Composable
fun PiPBuilderAddRemoteActions(
player: Player?,
shouldEnterPipMode: Boolean,
modifier: Modifier = Modifier,
) {
Expand All @@ -325,11 +327,11 @@ fun PiPBuilderAddRemoteActions(
listOfRemoteActions()
)

if (shouldEnterPipMode) {
if (shouldEnterPipMode && player != null && player.videoSize != VideoSize.UNKNOWN) {
val sourceRect = layoutCoordinates.boundsInWindow().toAndroidRectF().toRect()
builder.setSourceRectHint(sourceRect)
builder.setAspectRatio(
Rational(sourceRect.width(), sourceRect.height())
Rational(player.videoSize.width, player.videoSize.height)
)
}

Expand Down

0 comments on commit 0ce6be1

Please sign in to comment.