Skip to content

Commit

Permalink
pull lifecycle foo into PlaySurface
Browse files Browse the repository at this point in the history
  • Loading branch information
theScrabi committed Nov 1, 2024
1 parent 73ad522 commit 9f6c48d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import net.newpipe.newplayer.ui.ContentScale
Expand All @@ -46,10 +52,32 @@ import net.newpipe.newplayer.uiModel.NewPlayerUIState
internal fun PlaySurface(
modifier: Modifier,
player: Player?,
lifecycle: Lifecycle.Event,
uiState: NewPlayerUIState
) {

// init lifecycle foo for android view

val lifecycleOwner = androidx.lifecycle.compose.LocalLifecycleOwner.current

var lifecycle by remember {
mutableStateOf(Lifecycle.Event.ON_CREATE)
}

// Prepare stuff for the SurfaceView to which the video will be rendered
DisposableEffect(lifecycleOwner) {
val observer = LifecycleEventObserver { _, event ->
lifecycle = event
}
lifecycleOwner.lifecycle.addObserver(observer)

onDispose {
lifecycleOwner.lifecycle.removeObserver(observer)
}
}


// Take care of content ratio

val activity = LocalContext.current as Activity

val displayMetrics = activity.resources.displayMetrics
Expand All @@ -59,7 +87,7 @@ internal fun PlaySurface(

val fitMode = uiState.contentFitMode
val uiRatio = if (uiState.uiMode.fullscreen) screenRatio
else uiState.embeddedUiRatio
else uiState.embeddedUiRatio
val contentRatio = uiState.contentRatio

val internalModifier = modifier.aspectRatio(contentRatio)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,12 @@ internal fun VideoPlayerUi(viewModel: InternalNewPlayerViewModel, uiState: NewPl

val exoPlayer by viewModel.newPlayer?.exoPlayer!!.collectAsState()

var lifecycle by remember {
mutableStateOf(Lifecycle.Event.ON_CREATE)
}

val activity = LocalContext.current as Activity

val lifecycleOwner = androidx.lifecycle.compose.LocalLifecycleOwner.current

var videoViewBounds by remember {
mutableStateOf(android.graphics.Rect())
}

// Prepare stuff for the SurfaceView to which the video will be rendered
DisposableEffect(lifecycleOwner) {
val observer = LifecycleEventObserver { _, event ->
lifecycle = event
}
lifecycleOwner.lifecycle.addObserver(observer)

onDispose {
lifecycleOwner.lifecycle.removeObserver(observer)
}
}

LaunchedEffect(uiState.enteringPip) {
// TODO what if supportsPip returns false? Shouldn't the enteringPip flag be cleared?
// Probably the supportsPip check can be done in Application and then be available
Expand Down Expand Up @@ -131,7 +113,6 @@ internal fun VideoPlayerUi(viewModel: InternalNewPlayerViewModel, uiState: NewPl
.toRect()
},
player = exoPlayer,
lifecycle = lifecycle,
uiState = uiState
)
}
Expand Down

0 comments on commit 9f6c48d

Please sign in to comment.