fix(android): sideloaded subtitles (#4232)

This commit is contained in:
Krzysztof Moch 2024-10-11 22:50:22 +02:00 committed by GitHub
parent 78f4f0480d
commit 352dfbbc9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -32,6 +32,7 @@ import android.widget.TextView;
import androidx.activity.OnBackPressedCallback; import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread; import androidx.annotation.WorkerThread;
import androidx.media3.common.AudioAttributes; import androidx.media3.common.AudioAttributes;
import androidx.media3.common.C; import androidx.media3.common.C;
@ -140,6 +141,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@ -907,7 +909,7 @@ public class ReactExoplayerView extends FrameLayout implements
return; return;
} }
// init source to manage ads and external text tracks // init source to manage ads and external text tracks
ArrayList<MediaSource> mediaSourceList = buildTextSources(); MediaSource subtitlesSource = buildTextSource();
MediaSource videoSource = buildMediaSource(runningSource.getUri(), runningSource.getExtension(), drmSessionManager, runningSource.getCropStartMs(), runningSource.getCropEndMs()); MediaSource videoSource = buildMediaSource(runningSource.getUri(), runningSource.getExtension(), drmSessionManager, runningSource.getCropStartMs(), runningSource.getCropEndMs());
MediaSource mediaSourceWithAds = null; MediaSource mediaSourceWithAds = null;
Uri adTagUrl = null; Uri adTagUrl = null;
@ -923,22 +925,17 @@ public class ReactExoplayerView extends FrameLayout implements
exoPlayerView.showAds(); exoPlayerView.showAds();
} }
MediaSource mediaSource; MediaSource mediaSource;
if (mediaSourceList.isEmpty()) { if (subtitlesSource == null) {
if (mediaSourceWithAds != null) { mediaSource = Objects.requireNonNullElse(mediaSourceWithAds, videoSource);
mediaSource = mediaSourceWithAds;
} else {
mediaSource = videoSource;
}
} else { } else {
if (mediaSourceWithAds != null) { ArrayList<MediaSource> mediaSourceList = new ArrayList<>();
mediaSourceList.add(0, mediaSourceWithAds); mediaSourceList.add(subtitlesSource);
} else { mediaSourceList.add(0, Objects.requireNonNullElse(mediaSourceWithAds, videoSource));
mediaSourceList.add(0, videoSource); MediaSource[] mediaSourceArray = mediaSourceList.toArray(
}
MediaSource[] textSourceArray = mediaSourceList.toArray(
new MediaSource[mediaSourceList.size()] new MediaSource[mediaSourceList.size()]
); );
mediaSource = new MergingMediaSource(textSourceArray);
mediaSource = new MergingMediaSource(mediaSourceArray);
} }
// wait for player to be set // wait for player to be set
@ -1227,32 +1224,30 @@ public class ReactExoplayerView extends FrameLayout implements
return mediaSource; return mediaSource;
} }
private ArrayList<MediaSource> buildTextSources() { @Nullable
ArrayList<MediaSource> textSources = new ArrayList<>(); private MediaSource buildTextSource() {
if (source.getSideLoadedTextTracks() == null) { if (source.getSideLoadedTextTracks() == null) {
return textSources; return null;
} }
List<MediaItem.SubtitleConfiguration> subtitleConfigurations = new ArrayList<>();
for (SideLoadedTextTrack track : source.getSideLoadedTextTracks().getTracks()) { for (SideLoadedTextTrack track : source.getSideLoadedTextTracks().getTracks()) {
MediaSource textSource = buildTextSource(track.getTitle(), MediaItem.SubtitleConfiguration subtitleConfiguration = new MediaItem.SubtitleConfiguration.Builder(track.getUri())
track.getUri(), .setMimeType(track.getType())
track.getType(), .setLanguage(track.getLanguage())
track.getLanguage());
textSources.add(textSource);
}
return textSources;
}
private MediaSource buildTextSource(String title, Uri uri, String mimeType, String language) {
MediaItem.SubtitleConfiguration subtitleConfiguration = new MediaItem.SubtitleConfiguration.Builder(uri)
.setMimeType(mimeType)
.setLanguage(language)
.setSelectionFlags(C.SELECTION_FLAG_DEFAULT) .setSelectionFlags(C.SELECTION_FLAG_DEFAULT)
.setRoleFlags(C.ROLE_FLAG_SUBTITLE) .setRoleFlags(C.ROLE_FLAG_SUBTITLE)
.setLabel(title) .setLabel(track.getTitle())
.build(); .build();
return new SingleSampleMediaSource.Factory(mediaDataSourceFactory) subtitleConfigurations.add(subtitleConfiguration);
.createMediaSource(subtitleConfiguration, C.TIME_UNSET); }
MediaItem subtitlesMediaItem = new MediaItem.Builder()
.setUri(source.getUri())
.setSubtitleConfigurations(subtitleConfigurations).build();
return new DefaultMediaSourceFactory(mediaDataSourceFactory).createMediaSource(subtitlesMediaItem);
} }
private void releasePlayer() { private void releasePlayer() {