From 87a88e4df7f6c0040e5bfce735a24d7f8034b20d Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Sun, 19 Mar 2023 03:06:29 +0100 Subject: [PATCH] feat: localized audio track names --- app/build.gradle | 2 +- .../newpipe/player/PlayQueueActivity.java | 32 +++++++------- .../newpipe/player/ui/VideoPlayerUi.java | 13 ++---- .../org/schabi/newpipe/util/ListHelper.java | 11 ++++- .../org/schabi/newpipe/util/Localization.java | 42 +++++++++++++++++++ app/src/main/res/values/strings.xml | 6 ++- 6 files changed, 76 insertions(+), 30 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c0d423817..a766b16a8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -191,7 +191,7 @@ dependencies { // name and the commit hash with the commit hash of the (pushed) commit you want to test // This works thanks to JitPack: https://jitpack.io/ implementation 'com.github.TeamNewPipe:nanojson:1d9e1aea9049fc9f85e68b43ba39fe7be1c1f751' - implementation 'com.github.TeamNewPipe:NewPipeExtractor:5a9b6ed2e3306b9152cc6689dd61dbbe43483845' + implementation 'com.github.Theta-Dev:NewPipeExtractor:3fb356a7065c75909ee3856a29be92317c295bb9' implementation 'com.github.TeamNewPipe:NoNonsense-FilePicker:5.0.0' /** Checkstyle **/ diff --git a/app/src/main/java/org/schabi/newpipe/player/PlayQueueActivity.java b/app/src/main/java/org/schabi/newpipe/player/PlayQueueActivity.java index fa3668e8b..8c56627ec 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PlayQueueActivity.java +++ b/app/src/main/java/org/schabi/newpipe/player/PlayQueueActivity.java @@ -5,6 +5,7 @@ import static org.schabi.newpipe.player.helper.PlayerHelper.formatSpeed; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; import android.content.ComponentName; +import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; @@ -48,7 +49,6 @@ import org.schabi.newpipe.util.ServiceHelper; import org.schabi.newpipe.util.ThemeHelper; import java.util.List; -import java.util.Objects; import java.util.Optional; public final class PlayQueueActivity extends AppCompatActivity @@ -618,14 +618,17 @@ public final class PlayQueueActivity extends AppCompatActivity return; } + final Context context = player.getContext(); final MenuItem audioTrackSelector = menu.findItem(R.id.action_audio_track); final List availableStreams = Optional.ofNullable(player.getCurrentMetadata()) .flatMap(MediaItemTag::getMaybeAudioTrack) .map(MediaItemTag.AudioTrack::getAudioStreams) .orElse(null); + final Optional selectedAudioStream = player.getSelectedAudioStream(); - if (availableStreams == null || availableStreams.size() < 2) { + if (availableStreams == null || availableStreams.size() < 2 + || selectedAudioStream.isEmpty()) { audioTrackSelector.setVisible(false); } else { final SubMenu audioTrackMenu = audioTrackSelector.getSubMenu(); @@ -633,25 +636,20 @@ public final class PlayQueueActivity extends AppCompatActivity for (int i = 0; i < availableStreams.size(); i++) { final AudioStream audioStream = availableStreams.get(i); - if (audioStream.getAudioTrackName() == null) { - continue; - } - audioTrackMenu.add(MENU_ID_AUDIO_TRACK, i, Menu.NONE, - audioStream.getAudioTrackName()); + Localization.audioTrackName(context, audioStream)); } - player.getSelectedAudioStream().ifPresent(s -> { - final String trackName = Objects.toString(s.getAudioTrackName(), ""); - audioTrackSelector.setTitle(getString(R.string.play_queue_audio_track) + trackName); - - final String shortName = s.getAudioLocale() != null - ? s.getAudioLocale().getLanguage() : trackName; - audioTrackSelector.setTitleCondensed( - shortName.substring(0, Math.min(shortName.length(), 2))); - audioTrackSelector.setVisible(true); - }); + final AudioStream s = selectedAudioStream.get(); + final String trackName = Localization.audioTrackName(context, s); + audioTrackSelector.setTitle( + context.getString(R.string.play_queue_audio_track, trackName)); + final String shortName = s.getAudioLocale() != null + ? s.getAudioLocale().getLanguage() : trackName; + audioTrackSelector.setTitleCondensed( + shortName.substring(0, Math.min(shortName.length(), 2))); + audioTrackSelector.setVisible(true); } } diff --git a/app/src/main/java/org/schabi/newpipe/player/ui/VideoPlayerUi.java b/app/src/main/java/org/schabi/newpipe/player/ui/VideoPlayerUi.java index df966d591..8aff0af87 100644 --- a/app/src/main/java/org/schabi/newpipe/player/ui/VideoPlayerUi.java +++ b/app/src/main/java/org/schabi/newpipe/player/ui/VideoPlayerUi.java @@ -79,6 +79,7 @@ import org.schabi.newpipe.player.playqueue.PlayQueueItem; import org.schabi.newpipe.player.seekbarpreview.SeekbarPreviewThumbnailHelper; import org.schabi.newpipe.player.seekbarpreview.SeekbarPreviewThumbnailHolder; import org.schabi.newpipe.util.DeviceUtils; +import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.external_communication.KoreUtils; import org.schabi.newpipe.util.external_communication.ShareUtils; @@ -1093,15 +1094,13 @@ public abstract class VideoPlayerUi extends PlayerUi implements SeekBar.OnSeekBa for (int i = 0; i < availableStreams.size(); i++) { final AudioStream audioStream = availableStreams.get(i); - if (audioStream.getAudioTrackName() == null) { - continue; - } audioTrackPopupMenu.getMenu().add(POPUP_MENU_ID_AUDIO_TRACK, i, Menu.NONE, - audioStream.getAudioTrackName()); + Localization.audioTrackName(context, audioStream)); } player.getSelectedAudioStream() - .ifPresent(s -> binding.audioTrackTextView.setText(s.getAudioTrackName())); + .ifPresent(s -> binding.audioTrackTextView.setText( + Localization.audioTrackName(context, s))); binding.audioTrackTextView.setVisibility(View.VISIBLE); audioTrackPopupMenu.setOnMenuItemClickListener(this); audioTrackPopupMenu.setOnDismissListener(this); @@ -1218,10 +1217,6 @@ public abstract class VideoPlayerUi extends PlayerUi implements SeekBar.OnSeekBa private void onAudioTracksClicked() { audioTrackPopupMenu.show(); isSomePopupMenuVisible = true; - - player.getSelectedAudioStream() - .map(AudioStream::getAudioTrackName) - .ifPresent(binding.audioTrackTextView::setText); } /** diff --git a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java index a2e8216a3..971ee2759 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java @@ -13,6 +13,7 @@ import androidx.preference.PreferenceManager; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.MediaFormat; import org.schabi.newpipe.extractor.stream.AudioStream; +import org.schabi.newpipe.extractor.stream.AudioTrackType; import org.schabi.newpipe.extractor.stream.DeliveryMethod; import org.schabi.newpipe.extractor.stream.Stream; import org.schabi.newpipe.extractor.stream.VideoStream; @@ -244,8 +245,14 @@ public final class ListHelper { final Comparator trackCmp = getAudioTrackComparator(preferredLanguageOrEnglish, preferDescriptiveAudio); + // Filter unknown audio tracks if there are multiple tracks + java.util.stream.Stream cs = collectedStreams.values().stream(); + if (collectedStreams.size() > 1) { + cs = cs.filter(s -> s.getAudioTrackId() != null); + } + // Sort collected streams - return collectedStreams.values().stream().sorted(trackCmp).collect(Collectors.toList()); + return cs.sorted(trackCmp).collect(Collectors.toList()); } /*////////////////////////////////////////////////////////////////////////// @@ -672,7 +679,7 @@ public final class ListHelper { return Comparator.comparing(AudioStream::getAudioLocale, (o1, o2) -> Boolean.compare( o1 == null || !o1.getISO3Language().equals(preferredLanguage), o2 == null || !o2.getISO3Language().equals(preferredLanguage)) - ).thenComparing(AudioStream::isDescriptive, (o1, o2) -> + ).thenComparing(s -> s.getAudioTrackType() == AudioTrackType.DESCRIPTIVE, (o1, o2) -> Boolean.compare(o1 ^ preferDescriptiveAudio, o2 ^ preferDescriptiveAudio) ).thenComparing(AudioStream::getAudioTrackName, (o1, o2) -> { if (o1 != null) { diff --git a/app/src/main/java/org/schabi/newpipe/util/Localization.java b/app/src/main/java/org/schabi/newpipe/util/Localization.java index 916b902f0..b4745560c 100644 --- a/app/src/main/java/org/schabi/newpipe/util/Localization.java +++ b/app/src/main/java/org/schabi/newpipe/util/Localization.java @@ -21,6 +21,7 @@ import org.ocpsoft.prettytime.units.Decade; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.ListExtractor; import org.schabi.newpipe.extractor.localization.ContentCountry; +import org.schabi.newpipe.extractor.stream.AudioStream; import java.math.BigDecimal; import java.math.RoundingMode; @@ -261,6 +262,47 @@ public final class Localization { } } + /** + * Get the localized name of an audio track. + *

Example:

+ *

English (original)

+ *

English (descriptive)

+ *

Spanish (dubbed)

+ * + * @param context used to get app language + * @param track a {@link AudioStream} of the track + * @return localized track name + */ + public static String audioTrackName(final Context context, final AudioStream track) { + String res; + + if (track.getAudioLocale() != null) { + res = track.getAudioLocale().getDisplayLanguage(getAppLocale(context)); + } else if (track.getAudioTrackName() != null) { + res = track.getAudioTrackName(); + } else { + res = context.getString(R.string.unknown_audio_track); + } + + if (track.getAudioTrackType() != null) { + res += " ("; + switch (track.getAudioTrackType()) { + case ORIGINAL: + res += context.getString(R.string.track_type_original); + break; + case DUBBED: + res += context.getString(R.string.track_type_dubbed); + break; + case DESCRIPTIVE: + res += context.getString(R.string.track_type_descriptive); + break; + } + res += ")"; + } + + return res; + } + /*////////////////////////////////////////////////////////////////////////// // Pretty Time //////////////////////////////////////////////////////////////////////////*/ diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8c578eeb2..91d618852 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -413,7 +413,7 @@ Remove Details Audio Settings - Audio: + Audio: %s Audio track Hold to enqueue Show channel details @@ -768,10 +768,14 @@ Select quality for external players Unknown format Unknown quality + Unknown Show future items Hide future items Fully watched Partially watched Upcoming Sort + original + dubbed + descriptive \ No newline at end of file