Skip to content

Commit

Permalink
Don't destroy on dispose when using rememberPlayer(THEOplayerView)
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiasBuelens committed Sep 10, 2024
1 parent ea22f29 commit f59111f
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions ui/src/main/java/com/theoplayer/android/ui/UIController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,13 @@ fun rememberPlayer(config: THEOplayerConfig? = null): Player {
val context = LocalContext.current
remember { THEOplayerView(context, config) }
}

DisposableEffect(theoplayerView) {
onDispose {
theoplayerView?.onDestroy()
}
}

return rememberPlayerInternal(theoplayerView)
}

Expand All @@ -480,6 +487,17 @@ fun rememberPlayer(config: THEOplayerConfig? = null): Player {
* That is, it automatically calls [THEOplayerView.onPause] and [THEOplayerView.onResume]
* whenever the current activity is [paused][Activity.onPause] or [resumed][Activity.onResume].
*
* The [THEOplayerView] is **not** automatically destroyed when the composition is disposed.
* If you need this, use a [DisposableEffect]:
* ```kotlin
* val player = rememberPlayer(theoplayerView)
* DisposableEffect(theoplayerView) {
* onDispose {
* theoplayerView.onDestroy()
* }
* }
* ```
*
* @param theoplayerView the existing THEOplayer view
*/
@Composable
Expand All @@ -505,12 +523,6 @@ internal fun rememberPlayerInternal(theoplayerView: THEOplayerView?): Player {
internal fun setupTHEOplayerView(theoplayerView: THEOplayerView): THEOplayerView {
var wasPlayingAd by remember { mutableStateOf(false) }

DisposableEffect(theoplayerView) {
onDispose {
theoplayerView.onDestroy()
}
}

val lifecycle = LocalLifecycleOwner.current.lifecycle
DisposableEffect(lifecycle, theoplayerView) {
val lifecycleObserver = LifecycleEventObserver { _, event ->
Expand Down

0 comments on commit f59111f

Please sign in to comment.