mirror of
https://github.com/JustinZeus/Wholphin.git
synced 2026-07-08 23:51:21 +02:00
Add AmbientPlayerListener
This commit is contained in:
parent
e412c3da48
commit
993798d14b
3 changed files with 52 additions and 0 deletions
|
|
@ -1,8 +1,11 @@
|
|||
package com.github.damontecres.dolphin.ui
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import android.media.AudioManager
|
||||
import android.view.KeyEvent
|
||||
import android.view.WindowManager
|
||||
import androidx.compose.foundation.MarqueeAnimationMode
|
||||
import androidx.compose.foundation.basicMarquee
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
|
|
@ -229,3 +232,24 @@ fun Arrangement.spacedByWithFooter(space: Dp) =
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.findActivity(): Activity? {
|
||||
if (this is Activity) {
|
||||
return this
|
||||
}
|
||||
var context = this
|
||||
while (context is ContextWrapper) {
|
||||
if (context is Activity) return context
|
||||
context = context.baseContext
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun Activity.keepScreenOn(keep: Boolean) {
|
||||
Timber.v("Keep screen on: $keep")
|
||||
if (keep) {
|
||||
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||
} else {
|
||||
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
package com.github.damontecres.dolphin.ui.playback
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.media3.common.Player
|
||||
import androidx.media3.common.Player.Listener
|
||||
import com.github.damontecres.dolphin.ui.findActivity
|
||||
import com.github.damontecres.dolphin.ui.keepScreenOn
|
||||
|
||||
@Composable
|
||||
fun AmbientPlayerListener(player: Player) {
|
||||
val context = LocalContext.current
|
||||
DisposableEffect(player) {
|
||||
val listener =
|
||||
object : Listener {
|
||||
override fun onIsPlayingChanged(isPlaying: Boolean) {
|
||||
context.findActivity()?.keepScreenOn(isPlaying)
|
||||
}
|
||||
}
|
||||
player.addListener(listener)
|
||||
onDispose {
|
||||
player.removeListener(listener)
|
||||
context.findActivity()?.keepScreenOn(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -118,6 +118,7 @@ fun PlaybackContent(
|
|||
DisposableEffect(Unit) {
|
||||
onDispose { player.removeListener(cueListener) }
|
||||
}
|
||||
AmbientPlayerListener(player)
|
||||
|
||||
if (stream == null) {
|
||||
// TODO loading
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue