fix: some player track cannot be selected.

This commit is contained in:
oxy-macmini
2025-03-22 15:37:08 +08:00
parent 5696ada4a8
commit 2133184562
2 changed files with 27 additions and 30 deletions

View File

@ -8,14 +8,14 @@ import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
import androidx.media3.common.TrackGroup
import androidx.media3.common.Tracks
import com.m3u.data.database.model.Playlist
import com.m3u.data.database.model.Channel
import com.m3u.data.database.model.Playlist
import com.m3u.data.parser.xtream.XtreamChannelInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
interface PlayerManager {
val player: StateFlow<Player?>
@ -51,35 +51,34 @@ sealed class MediaCommand(open val channelId: Int) {
}
val PlayerManager.tracks: Flow<Map<@C.TrackType Int, List<Format>>>
get() = tracksGroups.map { all ->
get() = tracksGroups.mapLatest { all ->
// Group all tracks by their type
all.groupBy { it.type }
.mapValues { (_, innerGroups) ->
innerGroups
.map { group -> List(group.length) { group.getTrackFormat(it) } }
.flatten()
// For each group, flatten the list of tracks and filter out unsupported tracks
innerGroups.flatMap { trackGroup ->
List(trackGroup.length) { index ->
trackGroup.takeIf { it.isTrackSupported(index) }?.getTrackFormat(index)
}.filterNotNull()
}
}
}
.flowOn(Dispatchers.IO)
}.flowOn(Dispatchers.IO)
val PlayerManager.currentTracks: Flow<Map<@C.TrackType Int, Format?>>
get() = tracksGroups.map { all ->
all.groupBy { it.type }
.mapValues { (_, groups) ->
var format: Format? = null
outer@ for (group in groups) {
var selectedIndex = -1
inner@ for (i in 0 until group.length) {
if (group.isTrackSelected(i)) {
selectedIndex = i
break@inner
}
get() = tracksGroups.mapLatest { currentTracksGroups ->
currentTracksGroups
.groupBy { it.type }
.mapValues { (_, tracksGroups) ->
tracksGroups
.asSequence()
.mapNotNull { trackGroup ->
(0 until trackGroup.length)
.firstOrNull {
trackGroup.isTrackSupported(it) &&
trackGroup.isTrackSelected(it)
}
?.let { trackGroup.getTrackFormat(it) }
}
if (selectedIndex != -1) {
format = group.getTrackFormat(selectedIndex)
break@outer
}
}
format
.firstOrNull()
}
}
.flowOn(Dispatchers.IO)
}.flowOn(Dispatchers.IO)