Skip to content

Commit

Permalink
🎨 后台暂停
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoxieyoulei committed Apr 15, 2024
1 parent 9771f00 commit 002efd0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class EpgRepositoryImpl(private val context: Context) : EpgRepository {
return@flow
}

val xml = getXml()
val hashCode = filteredChannels.hashCode()

if (SP.epgCacheHash == hashCode) {
Expand All @@ -45,6 +44,7 @@ class EpgRepositoryImpl(private val context: Context) : EpgRepository {
}
}

val xml = getXml()
val epgList = parseFromXml(xml, filteredChannels)
val cacheFile = getCacheFile()
cacheFile.writeText(Json.encodeToString(epgList.value))
Expand Down Expand Up @@ -93,12 +93,12 @@ class EpgRepositoryImpl(private val context: Context) : EpgRepository {
if (dateFormat.format(System.currentTimeMillis()) == dateFormat.format(SP.epgXmlCacheTime)) {
val cache = getCacheXml()
if (cache.isNotBlank()) {
Log.d(IptvRepositoryImpl.TAG, "使用缓存xml")
Log.d(TAG, "使用缓存xml")
return cache
}
} else {
if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) < 1) {
Log.d(IptvRepositoryImpl.TAG, "未到1点,不刷新epg")
Log.d(TAG, "未到1点,不刷新epg")
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -25,6 +26,9 @@ import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.media3.common.C
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
Expand Down Expand Up @@ -69,6 +73,7 @@ fun HomeContent(
iptvGroupList = iptvGroupList,
),
) {
val lifecycleOwner = LocalLifecycleOwner.current
val focusRequester = remember { FocusRequester() }

val playerState = rememberExoPlayerState(state.exoPlayer)
Expand All @@ -84,6 +89,22 @@ fun HomeContent(
}
}

DisposableEffect(key1 = lifecycleOwner) {
val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_RESUME) {
state.exoPlayer.play()
} else if (event == Lifecycle.Event.ON_STOP) {
state.exoPlayer.pause()
}
}

lifecycleOwner.lifecycle.addObserver(observer)

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

BackPressHandledArea(
modifier = modifier,
onBackPressed = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,8 @@ fun VideoScreen(
SurfaceView(context)
},
update = { surfaceView ->
if (surfaceView.holder.surface.isValid) {
exoPlayer.setVideoSurface(surfaceView.holder.surface)
}
exoPlayer.setVideoSurfaceView(surfaceView)
},
onRelease = {
exoPlayer.release()
}
)
}

Expand All @@ -66,6 +61,11 @@ fun rememberExoPlayerState(

override fun onPlayerError(error: PlaybackException) {
state.error = true

if (error.errorCode == PlaybackException.ERROR_CODE_BEHIND_LIVE_WINDOW) {
exoPlayer.seekToDefaultPosition()
exoPlayer.prepare()
}
}

override fun onPlaybackStateChanged(playbackState: Int) {
Expand Down

0 comments on commit 002efd0

Please sign in to comment.