diff --git a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsSampleStreamWrapper.java b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsSampleStreamWrapper.java index fb1a6c53d9..2b1ec60607 100644 --- a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsSampleStreamWrapper.java +++ b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsSampleStreamWrapper.java @@ -1396,7 +1396,8 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; /** * Derives a track sample format from the corresponding format in the master playlist, and a - * sample format that may have been obtained from a chunk belonging to a different track. + * sample format that may have been obtained from a chunk belonging to a different track in the + * same track group. * * @param playlistFormat The format information obtained from the master playlist. * @param sampleFormat The format information obtained from the samples. @@ -1410,11 +1411,23 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; return sampleFormat; } - @Nullable - String codecs = - MimeTypes.getCodecsCorrespondingToMimeType( - playlistFormat.codecs, sampleFormat.sampleMimeType); - @Nullable String sampleMimeType = MimeTypes.getMediaMimeType(codecs); + int sampleTrackType = MimeTypes.getTrackType(sampleFormat.sampleMimeType); + @Nullable String sampleMimeType; + @Nullable String codecs; + if (Util.getCodecCountOfType(playlistFormat.codecs, sampleTrackType) == 1) { + // We can unequivocally map this track to a playlist variant because only one codec string + // matches this track's type. + codecs = Util.getCodecsOfType(playlistFormat.codecs, sampleTrackType); + sampleMimeType = MimeTypes.getMediaMimeType(codecs); + } else { + // The variant assigns more than one codec string to this track. We choose whichever codec + // string matches the sample mime type. This can happen when different languages are encoded + // using different codecs. + codecs = + MimeTypes.getCodecsCorrespondingToMimeType( + playlistFormat.codecs, sampleFormat.sampleMimeType); + sampleMimeType = sampleFormat.sampleMimeType; + } Format.Builder formatBuilder = sampleFormat