Skip to content

Commit

Permalink
Workaround for RememberObserver issue
Browse files Browse the repository at this point in the history
  • Loading branch information
fengdai committed May 27, 2022
1 parent de29aa1 commit 7fde9d9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,21 @@ fun BasicContent(
val player by rememberManagedExoPlayer { context ->
setMediaSourceFactory(ProgressiveMediaSource.Factory(DefaultDataSource.Factory(context)))
}
SideEffect { player?.playWhenReady = playWhenReady }
DisposableEffect(player, playWhenReady) {
player?.playWhenReady = playWhenReady
onDispose {}
}

val mediaItem = remember(url) { MediaItem.fromUri(url) }
LaunchedEffect(mediaItem, player) {
DisposableEffect(mediaItem, player) {
player?.run {
setMediaItem(mediaItem)
prepare()
}
onDispose {}
}

val state = rememberUpdatedMediaState(player = player.takeIf { setPlayer })

val state = rememberMediaState()
val media = @Composable {
Media(
state,
Expand Down Expand Up @@ -127,15 +130,11 @@ fun BasicContent(
},
controller = when (controllerType) {
ControllerType.None -> null
ControllerType.Simple -> {
@Composable { state ->
SimpleController(state, Modifier.fillMaxSize())
}
ControllerType.Simple -> @Composable { state ->
SimpleController(state, Modifier.fillMaxSize())
}
ControllerType.StyledPlayerControlView -> {
@Composable { state ->
StyledPlayerControlViewController(state, Modifier.fillMaxSize())
}
ControllerType.StyledPlayerControlView -> @Composable { state ->
StyledPlayerControlViewController(state, Modifier.fillMaxSize())
}
}
)
Expand Down Expand Up @@ -168,11 +167,16 @@ fun BasicContent(
}
}

val playerToSet = player.takeIf { setPlayer }
val isLandscape = LocalConfiguration.current.orientation == Configuration.ORIENTATION_LANDSCAPE
if (!isLandscape) Column(modifier) {
// TODO Workaround for RememberObserver issue.
// https://kotlinlang.slack.com/archives/CJLTWPH7S/p1653543177516939
state.playerState = playerToSet?.run { rememberPlayerState(player = this) }
media()
options()
} else Row(modifier) {
state.playerState = playerToSet?.run { rememberPlayerState(player = this) }
media()
options()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavHostController
import com.github.fengdai.compose.media.Media
import com.github.fengdai.compose.media.MediaState
import com.github.fengdai.compose.media.rememberUpdatedMediaState
import com.github.fengdai.compose.media.*
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.source.ProgressiveMediaSource
Expand Down Expand Up @@ -50,9 +48,11 @@ fun FullscreenToggle(navController: NavHostController) {
}
}

val state = rememberUpdatedMediaState(player = player)

val state = rememberMediaState()
if (!isLandscape) {
// TODO Workaround for RememberObserver issue.
// https://kotlinlang.slack.com/archives/CJLTWPH7S/p1653543177516939
state.playerState = player?.run { rememberPlayerState(player = this) }
NormalContent(
state,
onBackPressed = { navController.popBackStack() },
Expand All @@ -62,6 +62,7 @@ fun FullscreenToggle(navController: NavHostController) {
}
)
} else {
state.playerState = player?.run { rememberPlayerState(player = this) }
FullscreenContent(
state,
activity.onBackPressedDispatcher,
Expand Down

0 comments on commit 7fde9d9

Please sign in to comment.