Fix audio track selection if multiple renderers are used

When ExoPlayer has to use multiple different renderers, the order of mapped track groups returned by the player may not conform to the order of media streams in the media source.
Unfortunately, ExoPlayer doesn't allow accessing the original TrackGroups that are kept in the MediaPeriod, thus we instead simply sort the available groups by the format id.
This commit is contained in:
Maxr1998 authored and Niels van Velzen committed 2023-04-28 20:56:47 +02:00
1 parent cf576b9345
commit 6547b727fe
1 file changed
+7 -4
@@ -70,9 +70,12 @@ class TrackSelectionHelper(
val player = viewModel.playerOrNull ?: return false
val embeddedStreamIndex = mediaSource.getEmbeddedStreamIndex(audioStream)
val audioGroup = player.currentTracks.groups.getOrNull(embeddedStreamIndex)?.mediaTrackGroup ?: return false
val sortedTrackGroups = player.currentTracks.groups.sortedBy { group ->
group.mediaTrackGroup.getFormat(0).id
}
val audioGroup = sortedTrackGroups.getOrNull(embeddedStreamIndex) ?: return false
return trackSelector.selectTrackByTypeAndGroup(C.TRACK_TYPE_AUDIO, audioGroup)
return trackSelector.selectTrackByTypeAndGroup(C.TRACK_TYPE_AUDIO, audioGroup.mediaTrackGroup)
}
/**
@@ -132,9 +135,9 @@ class TrackSelectionHelper(
SubtitleDeliveryMethod.EMBED -> {
// For embedded subtitles, we can match by the index of this stream in all embedded streams.
val embeddedStreamIndex = mediaSource.getEmbeddedStreamIndex(subtitleStream)
val subtitleGroup = player.currentTracks.groups.getOrNull(embeddedStreamIndex)?.mediaTrackGroup ?: return false
val subtitleGroup = player.currentTracks.groups.getOrNull(embeddedStreamIndex) ?: return false
return trackSelector.selectTrackByTypeAndGroup(C.TRACK_TYPE_TEXT, subtitleGroup)
return trackSelector.selectTrackByTypeAndGroup(C.TRACK_TYPE_TEXT, subtitleGroup.mediaTrackGroup)
}
SubtitleDeliveryMethod.EXTERNAL -> {
// For external subtitles, we can simply match the ID that we set when creating the player media source.