Fixing the Notification order for multiple playbacks
This commit is contained in:
+1
-1
@@ -48,7 +48,7 @@ object BackgroundMedia {
|
|||||||
// background playing mutex.
|
// background playing mutex.
|
||||||
val bgInstance = MutableStateFlow<MediaControllerState?>(null)
|
val bgInstance = MutableStateFlow<MediaControllerState?>(null)
|
||||||
|
|
||||||
private fun hasInstance() = bgInstance.value != null
|
fun hasInstance() = bgInstance.value != null
|
||||||
|
|
||||||
fun isPlaying() = bgInstance.value?.isPlaying() == true
|
fun isPlaying() = bgInstance.value?.isPlaying() == true
|
||||||
|
|
||||||
|
|||||||
+2
@@ -165,6 +165,8 @@ class MediaSessionPool(
|
|||||||
|
|
||||||
fun playingContent() = playingMap.values
|
fun playingContent() = playingMap.values
|
||||||
|
|
||||||
|
fun getSession(id: String) = cache.get(id)?.session
|
||||||
|
|
||||||
class MediaSessionCallback(
|
class MediaSessionCallback(
|
||||||
val pool: MediaSessionPool,
|
val pool: MediaSessionPool,
|
||||||
) : MediaSession.Callback {
|
) : MediaSession.Callback {
|
||||||
|
|||||||
+27
-17
@@ -29,6 +29,7 @@ import androidx.media3.exoplayer.ExoPlayer
|
|||||||
import androidx.media3.session.MediaSession
|
import androidx.media3.session.MediaSession
|
||||||
import androidx.media3.session.MediaSessionService
|
import androidx.media3.session.MediaSessionService
|
||||||
import com.vitorpamplona.amethyst.service.okhttp.HttpClientManager
|
import com.vitorpamplona.amethyst.service.okhttp.HttpClientManager
|
||||||
|
import com.vitorpamplona.amethyst.service.playback.pip.BackgroundMedia
|
||||||
import com.vitorpamplona.amethyst.service.playback.playerPool.ExoPlayerBuilder
|
import com.vitorpamplona.amethyst.service.playback.playerPool.ExoPlayerBuilder
|
||||||
import com.vitorpamplona.amethyst.service.playback.playerPool.ExoPlayerPool
|
import com.vitorpamplona.amethyst.service.playback.playerPool.ExoPlayerPool
|
||||||
import com.vitorpamplona.amethyst.service.playback.playerPool.MediaSessionPool
|
import com.vitorpamplona.amethyst.service.playback.playerPool.MediaSessionPool
|
||||||
@@ -91,37 +92,46 @@ class PlaybackService : MediaSessionService() {
|
|||||||
// Updates any new player ready
|
// Updates any new player ready
|
||||||
super.onUpdateNotification(session, startInForegroundRequired)
|
super.onUpdateNotification(session, startInForegroundRequired)
|
||||||
|
|
||||||
val proxyPlaying = poolWithProxy?.playingContent()
|
// playback controllers control the last notification updated.
|
||||||
|
// this procedure re-updates the notification to make sure it aligns
|
||||||
|
// with users expectation on which playback they decide to control:
|
||||||
|
// 1. If no video is being played, play the picture in picture if there.
|
||||||
|
// 2. If there are videos being played the order is:
|
||||||
|
// 2. a. Picture in picture if playing
|
||||||
|
// 2. b. On screen video with volume on
|
||||||
|
// 2. c. On screen video with volume off.
|
||||||
|
|
||||||
// Overrides the notification with any player actually playing
|
val playing = (poolWithProxy?.playingContent() ?: emptyList()) + (poolNoProxy?.playingContent() ?: emptyList())
|
||||||
proxyPlaying?.forEach {
|
|
||||||
if (it.session.player.isPlaying) {
|
// if nothing is pl
|
||||||
|
if (playing.isEmpty() && BackgroundMedia.hasInstance()) {
|
||||||
|
BackgroundMedia.bgInstance.value?.id?.let { id ->
|
||||||
|
(poolNoProxy?.getSession(id) ?: poolWithProxy?.getSession(id))?.let {
|
||||||
|
super.onUpdateNotification(it, startInForegroundRequired)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
playing.forEachIndexed { idx, it ->
|
||||||
|
if (it.session.player.isPlaying && it.session.player.volume > 0 && it.session.id == BackgroundMedia.bgInstance.value?.id) {
|
||||||
super.onUpdateNotification(it.session, startInForegroundRequired)
|
super.onUpdateNotification(it.session, startInForegroundRequired)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overrides again with playing with audio
|
playing.forEachIndexed { idx, it ->
|
||||||
proxyPlaying?.forEach {
|
|
||||||
if (it.session.player.isPlaying && it.session.player.volume > 0) {
|
if (it.session.player.isPlaying && it.session.player.volume > 0) {
|
||||||
super.onUpdateNotification(it.session, startInForegroundRequired)
|
super.onUpdateNotification(it.session, startInForegroundRequired)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val noProxyPlaying = poolNoProxy?.playingContent()
|
playing.forEachIndexed { idx, it ->
|
||||||
|
|
||||||
// Overrides the notification with any player actually playing
|
|
||||||
noProxyPlaying?.forEach {
|
|
||||||
if (it.session.player.isPlaying) {
|
if (it.session.player.isPlaying) {
|
||||||
super.onUpdateNotification(it.session, startInForegroundRequired)
|
super.onUpdateNotification(it.session, startInForegroundRequired)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overrides again with playing with audio
|
|
||||||
noProxyPlaying?.forEach {
|
|
||||||
if (it.session.player.isPlaying && it.session.player.volume > 0) {
|
|
||||||
super.onUpdateNotification(it.session, startInForegroundRequired)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return a MediaSession to link with the MediaController that is making
|
// Return a MediaSession to link with the MediaController that is making
|
||||||
|
|||||||
Reference in New Issue
Block a user