diff --git a/demos/cast/src/main/java/androidx/media3/demo/cast/PlayerManager.java b/demos/cast/src/main/java/androidx/media3/demo/cast/PlayerManager.java index 89910104f6..8f4d122f92 100644 --- a/demos/cast/src/main/java/androidx/media3/demo/cast/PlayerManager.java +++ b/demos/cast/src/main/java/androidx/media3/demo/cast/PlayerManager.java @@ -223,12 +223,12 @@ import java.util.ArrayList; if (currentPlayer != localPlayer || tracksInfo == lastSeenTrackGroupInfo) { return; } - if (!tracksInfo.isTypeSupportedOrEmpty( - C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) { + if (tracksInfo.containsType(C.TRACK_TYPE_VIDEO) + && !tracksInfo.isTypeSupported(C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) { listener.onUnsupportedTrack(C.TRACK_TYPE_VIDEO); } - if (!tracksInfo.isTypeSupportedOrEmpty( - C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) { + if (tracksInfo.containsType(C.TRACK_TYPE_AUDIO) + && !tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) { listener.onUnsupportedTrack(C.TRACK_TYPE_AUDIO); } lastSeenTrackGroupInfo = tracksInfo; diff --git a/demos/main/src/main/java/androidx/media3/demo/main/PlayerActivity.java b/demos/main/src/main/java/androidx/media3/demo/main/PlayerActivity.java index 437fedbdc7..fc82d5aa1b 100644 --- a/demos/main/src/main/java/androidx/media3/demo/main/PlayerActivity.java +++ b/demos/main/src/main/java/androidx/media3/demo/main/PlayerActivity.java @@ -466,12 +466,14 @@ public class PlayerActivity extends AppCompatActivity if (tracksInfo == lastSeenTracksInfo) { return; } - if (!tracksInfo.isTypeSupportedOrEmpty( - C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) { + if (tracksInfo.containsType(C.TRACK_TYPE_VIDEO) + && !tracksInfo.isTypeSupported( + C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) { showToast(R.string.error_unsupported_video); } - if (!tracksInfo.isTypeSupportedOrEmpty( - C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) { + if (tracksInfo.containsType(C.TRACK_TYPE_AUDIO) + && !tracksInfo.isTypeSupported( + C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) { showToast(R.string.error_unsupported_audio); } lastSeenTracksInfo = tracksInfo; diff --git a/libraries/common/src/main/java/androidx/media3/common/TracksInfo.java b/libraries/common/src/main/java/androidx/media3/common/TracksInfo.java index 9dd0885788..2405bb207c 100644 --- a/libraries/common/src/main/java/androidx/media3/common/TracksInfo.java +++ b/libraries/common/src/main/java/androidx/media3/common/TracksInfo.java @@ -288,7 +288,7 @@ public final class TracksInfo implements Bundleable { } /** Returns true if there are tracks of type {@code trackType}, and false otherwise. */ - public boolean hasTracksOfType(@C.TrackType int trackType) { + public boolean containsType(@C.TrackType int trackType) { for (int i = 0; i < trackGroupInfos.size(); i++) { if (trackGroupInfos.get(i).getTrackType() == trackType) { return true; @@ -299,16 +299,15 @@ public final class TracksInfo implements Bundleable { /** * Returns true if at least one track of type {@code trackType} is {@link - * TrackGroupInfo#isTrackSupported(int) supported} or if there are no tracks of this type. + * TrackGroupInfo#isTrackSupported(int) supported}. */ - public boolean isTypeSupportedOrEmpty(@C.TrackType int trackType) { - return isTypeSupportedOrEmpty(trackType, /* allowExceedsCapabilities= */ false); + public boolean isTypeSupported(@C.TrackType int trackType) { + return isTypeSupported(trackType, /* allowExceedsCapabilities= */ false); } /** * Returns true if at least one track of type {@code trackType} is {@link - * TrackGroupInfo#isTrackSupported(int, boolean) supported} or if there are no tracks of this - * type. + * TrackGroupInfo#isTrackSupported(int, boolean) supported}. * * @param allowExceedsCapabilities Whether to consider the track as supported if it has a * supported {@link Format#sampleMimeType MIME type}, but otherwise exceeds the advertised @@ -316,19 +315,31 @@ public final class TracksInfo implements Bundleable { * decoder whose maximum advertised resolution is exceeded by the resolution of the track. * Such tracks may be playable in some cases. */ - public boolean isTypeSupportedOrEmpty( - @C.TrackType int trackType, boolean allowExceedsCapabilities) { - boolean supported = true; + public boolean isTypeSupported(@C.TrackType int trackType, boolean allowExceedsCapabilities) { for (int i = 0; i < trackGroupInfos.size(); i++) { if (trackGroupInfos.get(i).getTrackType() == trackType) { if (trackGroupInfos.get(i).isSupported(allowExceedsCapabilities)) { return true; - } else { - supported = false; } } } - return supported; + return false; + } + + /** @deprecated Use {@link #containsType(int)} and {@link #isTypeSupported(int)}. */ + @Deprecated + @UnstableApi + @SuppressWarnings("deprecation") + public boolean isTypeSupportedOrEmpty(@C.TrackType int trackType) { + return isTypeSupportedOrEmpty(trackType, /* allowExceedsCapabilities= */ false); + } + + /** @deprecated Use {@link #containsType(int)} and {@link #isTypeSupported(int, boolean)}. */ + @Deprecated + @UnstableApi + public boolean isTypeSupportedOrEmpty( + @C.TrackType int trackType, boolean allowExceedsCapabilities) { + return !containsType(trackType) || isTypeSupported(trackType, allowExceedsCapabilities); } /** Returns true if at least one track of the type {@code trackType} is selected for playback. */ diff --git a/libraries/common/src/test/java/androidx/media3/common/TracksInfoTest.java b/libraries/common/src/test/java/androidx/media3/common/TracksInfoTest.java index c3f4182e29..d9e1aced24 100644 --- a/libraries/common/src/test/java/androidx/media3/common/TracksInfoTest.java +++ b/libraries/common/src/test/java/androidx/media3/common/TracksInfoTest.java @@ -61,12 +61,10 @@ public class TracksInfoTest { public void tracksInfoGetters_withoutTrack_returnExpectedValues() { TracksInfo tracksInfo = new TracksInfo(ImmutableList.of()); - assertThat(tracksInfo.hasTracksOfType(C.TRACK_TYPE_AUDIO)).isFalse(); - assertThat(tracksInfo.isTypeSupportedOrEmpty(C.TRACK_TYPE_AUDIO)).isTrue(); - assertThat( - tracksInfo.isTypeSupportedOrEmpty( - C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) - .isTrue(); + assertThat(tracksInfo.containsType(C.TRACK_TYPE_AUDIO)).isFalse(); + assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO)).isFalse(); + assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) + .isFalse(); assertThat(tracksInfo.isTypeSelected(C.TRACK_TYPE_AUDIO)).isFalse(); ImmutableList trackGroupInfos = tracksInfo.getTrackGroupInfos(); assertThat(trackGroupInfos).isEmpty(); @@ -98,24 +96,18 @@ public class TracksInfoTest { /* tracksSelected= */ new boolean[] {false, true}); TracksInfo tracksInfo = new TracksInfo(ImmutableList.of(trackGroupInfo0, trackGroupInfo1)); - assertThat(tracksInfo.hasTracksOfType(C.TRACK_TYPE_AUDIO)).isTrue(); - assertThat(tracksInfo.hasTracksOfType(C.TRACK_TYPE_VIDEO)).isTrue(); - assertThat(tracksInfo.hasTracksOfType(C.TRACK_TYPE_TEXT)).isFalse(); - assertThat(tracksInfo.isTypeSupportedOrEmpty(C.TRACK_TYPE_AUDIO)).isFalse(); - assertThat(tracksInfo.isTypeSupportedOrEmpty(C.TRACK_TYPE_VIDEO)).isTrue(); - assertThat(tracksInfo.isTypeSupportedOrEmpty(C.TRACK_TYPE_TEXT)).isTrue(); - assertThat( - tracksInfo.isTypeSupportedOrEmpty( - C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) + assertThat(tracksInfo.containsType(C.TRACK_TYPE_AUDIO)).isTrue(); + assertThat(tracksInfo.containsType(C.TRACK_TYPE_VIDEO)).isTrue(); + assertThat(tracksInfo.containsType(C.TRACK_TYPE_TEXT)).isFalse(); + assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO)).isFalse(); + assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_VIDEO)).isTrue(); + assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_TEXT)).isFalse(); + assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) .isTrue(); - assertThat( - tracksInfo.isTypeSupportedOrEmpty( - C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) - .isTrue(); - assertThat( - tracksInfo.isTypeSupportedOrEmpty( - C.TRACK_TYPE_TEXT, /* allowExceedsCapabilities= */ true)) + assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) .isTrue(); + assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_TEXT, /* allowExceedsCapabilities= */ true)) + .isFalse(); assertThat(tracksInfo.isTypeSelected(C.TRACK_TYPE_AUDIO)).isFalse(); assertThat(tracksInfo.isTypeSelected(C.TRACK_TYPE_VIDEO)).isTrue(); ImmutableList trackGroupInfos = tracksInfo.getTrackGroupInfos();