From f506fc0478cbed1df0387880c9b1513d0f233169 Mon Sep 17 00:00:00 2001 From: John Zhen Mo Date: Fri, 9 Feb 2018 12:43:24 -0800 Subject: [PATCH] -Moved caption extraction and menu building into exoplayer track changing callback. -Updated extractor dependency. --- .../newpipe/player/PopupVideoPlayer.java | 2 +- .../schabi/newpipe/player/VideoPlayer.java | 89 +++++++++++++------ 2 files changed, 61 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java index bf6d6f445..9b5413977 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java @@ -676,7 +676,7 @@ public final class PopupVideoPlayer extends Service { /*package-private*/ void enableVideoRenderer(final boolean enable) { final int videoRendererIndex = getRendererIndex(C.TRACK_TYPE_VIDEO); - if (trackSelector != null && videoRendererIndex != -1) { + if (trackSelector != null && videoRendererIndex != RENDERER_UNAVAILABLE) { trackSelector.setRendererDisabled(videoRendererIndex, !enable); } } diff --git a/app/src/main/java/org/schabi/newpipe/player/VideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/VideoPlayer.java index 344175030..7f41c028f 100644 --- a/app/src/main/java/org/schabi/newpipe/player/VideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/VideoPlayer.java @@ -53,6 +53,7 @@ import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.source.MediaSource; import com.google.android.exoplayer2.source.MergingMediaSource; import com.google.android.exoplayer2.source.SingleSampleMediaSource; +import com.google.android.exoplayer2.source.TrackGroup; import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.trackselection.TrackSelectionArray; import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; @@ -98,6 +99,7 @@ public abstract class VideoPlayer extends BasePlayer // Player //////////////////////////////////////////////////////////////////////////*/ + protected static final int RENDERER_UNAVAILABLE = -1; public static final int DEFAULT_CONTROLS_HIDE_TIME = 2000; // 2 Seconds private ArrayList availableStreams; @@ -105,8 +107,6 @@ public abstract class VideoPlayer extends BasePlayer protected String playbackQuality; - private List availableCaptions; - protected boolean wasPlaying = false; /*////////////////////////////////////////////////////////////////////////// @@ -271,35 +271,32 @@ public abstract class VideoPlayer extends BasePlayer playbackSpeedPopupMenu.setOnDismissListener(this); } - private void buildCaptionMenu() { + private void buildCaptionMenu(final List availableLanguages) { if (captionPopupMenu == null) return; captionPopupMenu.getMenu().removeGroup(captionPopupMenuGroupId); - if (availableCaptions == null || trackSelector == null) return; - // Add option for turning off caption MenuItem captionOffItem = captionPopupMenu.getMenu().add(captionPopupMenuGroupId, 0, Menu.NONE, R.string.caption_none); captionOffItem.setOnMenuItemClickListener(menuItem -> { final int textRendererIndex = getRendererIndex(C.TRACK_TYPE_TEXT); - if (trackSelector != null && textRendererIndex != -1) { + if (trackSelector != null && textRendererIndex != RENDERER_UNAVAILABLE) { trackSelector.setRendererDisabled(textRendererIndex, true); } return true; }); // Add all available captions - for (int i = 0; i < availableCaptions.size(); i++) { - final Subtitles subtitles = availableCaptions.get(i); - final String captionLanguage = PlayerHelper.captionLanguageOf(subtitles); + for (int i = 0; i < availableLanguages.size(); i++) { + final String captionLanguage = availableLanguages.get(i); MenuItem captionItem = captionPopupMenu.getMenu().add(captionPopupMenuGroupId, i + 1, Menu.NONE, captionLanguage); captionItem.setOnMenuItemClickListener(menuItem -> { final int textRendererIndex = getRendererIndex(C.TRACK_TYPE_TEXT); - if (trackSelector != null && textRendererIndex != -1) { - trackSelector.setRendererDisabled(textRendererIndex, false); + if (trackSelector != null && textRendererIndex != RENDERER_UNAVAILABLE) { trackSelector.setParameters(trackSelector.getParameters() .withPreferredTextLanguage(captionLanguage)); + trackSelector.setRendererDisabled(textRendererIndex, false); } return true; }); @@ -319,7 +316,6 @@ public abstract class VideoPlayer extends BasePlayer super.sync(item, info); qualityTextView.setVisibility(View.GONE); playbackSpeedTextView.setVisibility(View.GONE); - captionTextView.setVisibility(View.GONE); if (info != null) { final List videos = ListHelper.getSortedStreamVideosList(context, @@ -331,14 +327,10 @@ public abstract class VideoPlayer extends BasePlayer selectedStreamIndex = getOverrideResolutionIndex(videos, getPlaybackQuality()); } - availableCaptions = info.getSubtitles(); - buildQualityMenu(); buildPlaybackSpeedMenu(); - buildCaptionMenu(); qualityTextView.setVisibility(View.VISIBLE); playbackSpeedTextView.setVisibility(View.VISIBLE); - captionTextView.setVisibility(availableCaptions.isEmpty() ? View.GONE : View.VISIBLE); } } @@ -469,17 +461,7 @@ public abstract class VideoPlayer extends BasePlayer @Override public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) { super.onTracksChanged(trackGroups, trackSelections); - if (captionTextView == null) return; - - if (trackSelector == null) { - captionTextView.setVisibility(View.GONE); - } else if (trackSelector.getRendererDisabled(getRendererIndex(C.TRACK_TYPE_TEXT)) || - trackSelector.getParameters().preferredTextLanguage == null) { - captionTextView.setText(R.string.caption_none); - } else { - final String preferredLanguage = trackSelector.getParameters().preferredTextLanguage; - captionTextView.setText(preferredLanguage); - } + onTextTrackUpdate(); } @Override @@ -495,6 +477,55 @@ public abstract class VideoPlayer extends BasePlayer animateView(surfaceForeground, false, 100); } + /*////////////////////////////////////////////////////////////////////////// + // ExoPlayer Track Updates + //////////////////////////////////////////////////////////////////////////*/ + + private void onTextTrackUpdate() { + final int textRenderer = getRendererIndex(C.TRACK_TYPE_TEXT); + + if (captionTextView == null) return; + if (trackSelector == null || trackSelector.getCurrentMappedTrackInfo() == null || + textRenderer == RENDERER_UNAVAILABLE) { + captionTextView.setVisibility(View.GONE); + return; + } + + final TrackGroupArray textTracks = trackSelector.getCurrentMappedTrackInfo() + .getTrackGroups(textRenderer); + + // Extract all loaded languages + List availableLanguages = new ArrayList<>(textTracks.length); + for (int i = 0; i < textTracks.length; i++) { + final TrackGroup textTrack = textTracks.get(i); + if (textTrack.length > 0 && textTrack.getFormat(0) != null) { + availableLanguages.add(textTrack.getFormat(0).language); + } + } + + // Normalize mismatching language strings + final String preferredLanguage = trackSelector.getParameters().preferredTextLanguage; + // Because ExoPlayer normalizes the preferred language string but not the text track + // language strings, some preferred language string will have the language name in lowercase + String formattedPreferredLanguage = null; + if (preferredLanguage != null) { + for (final String language : availableLanguages) { + if (language.compareToIgnoreCase(preferredLanguage) == 0) + formattedPreferredLanguage = language; + } + } + + // Build UI + buildCaptionMenu(availableLanguages); + if (trackSelector.getRendererDisabled(textRenderer) || formattedPreferredLanguage == null || + !availableLanguages.contains(formattedPreferredLanguage)) { + captionTextView.setText(R.string.caption_none); + } else { + captionTextView.setText(formattedPreferredLanguage); + } + captionTextView.setVisibility(availableLanguages.isEmpty() ? View.GONE : View.VISIBLE); + } + /*////////////////////////////////////////////////////////////////////////// // General Player //////////////////////////////////////////////////////////////////////////*/ @@ -688,7 +719,7 @@ public abstract class VideoPlayer extends BasePlayer //////////////////////////////////////////////////////////////////////////*/ public int getRendererIndex(final int trackIndex) { - if (simpleExoPlayer == null) return -1; + if (simpleExoPlayer == null) return RENDERER_UNAVAILABLE; for (int t = 0; t < simpleExoPlayer.getRendererCount(); t++) { if (simpleExoPlayer.getRendererType(t) == trackIndex) { @@ -696,7 +727,7 @@ public abstract class VideoPlayer extends BasePlayer } } - return -1; + return RENDERER_UNAVAILABLE; } public boolean isControlsVisible() {