Skip to content

Commit

Permalink
Fix audio track selection if multiple renderers are used
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Maxr1998 authored and nielsvanvelzen committed Apr 28, 2023
1 parent cf576b9 commit 6547b72
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6547b72

Please sign in to comment.