Skip to content

Commit bebd697

Browse files
committed
use Modifier.onVisibilityChanged to listen for whether the composable is completely visible or not, and play or pause the video based on this information
1 parent 5b324d1 commit bebd697

File tree

1 file changed

+5
-16
lines changed
  • feature/home/src/main/java/com/android/developers/androidify/home

1 file changed

+5
-16
lines changed

feature/home/src/main/java/com/android/developers/androidify/home/HomeScreen.kt

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import androidx.compose.ui.graphics.graphicsLayer
7676
import androidx.compose.ui.graphics.vector.ImageVector
7777
import androidx.compose.ui.graphics.vector.rememberVectorPainter
7878
import androidx.compose.ui.layout.onLayoutRectChanged
79+
import androidx.compose.ui.layout.onVisibilityChanged
7980
import androidx.compose.ui.platform.LocalContext
8081
import androidx.compose.ui.platform.LocalInspectionMode
8182
import androidx.compose.ui.platform.LocalView
@@ -541,9 +542,10 @@ private fun VideoPlayer(
541542
Modifier
542543
.background(MaterialTheme.colorScheme.surfaceContainerLowest)
543544
.onVisibilityChanged(
544-
containerWidth = LocalView.current.width,
545-
containerHeight = LocalView.current.height,
546-
) { fullyVisible -> videoFullyOnScreen = fullyVisible }
545+
minDurationMs = 100,
546+
minFractionVisible = 1f
547+
)
548+
{ fullyVisible -> videoFullyOnScreen = fullyVisible }
547549
.then(modifier),
548550
) {
549551
player?.let { currentPlayer ->
@@ -579,16 +581,3 @@ private fun VideoPlayer(
579581
}
580582
}
581583
}
582-
583-
fun Modifier.onVisibilityChanged(
584-
containerWidth: Int,
585-
containerHeight: Int,
586-
onChanged: (visible: Boolean) -> Unit,
587-
) = this then Modifier.onLayoutRectChanged(100, 0) { layoutBounds ->
588-
onChanged(
589-
layoutBounds.boundsInRoot.top > 0 &&
590-
layoutBounds.boundsInRoot.bottom < containerHeight &&
591-
layoutBounds.boundsInRoot.left > 0 &&
592-
layoutBounds.boundsInRoot.right < containerWidth,
593-
)
594-
}

0 commit comments

Comments
 (0)