From 25b453a5fe86e1964585bcdd945450f0532d192e Mon Sep 17 00:00:00 2001 From: samrobinson Date: Mon, 17 May 2021 19:05:54 +0100 Subject: [PATCH] Add track number & total to MediaMetadata #minor-release PiperOrigin-RevId: 374235979 --- .../android/exoplayer2/MediaMetadata.java | 49 +++++++++++++++++-- .../metadata/id3/TextInformationFrame.java | 13 +++++ .../android/exoplayer2/MediaMetadataTest.java | 31 ++++++++++-- 3 files changed, 85 insertions(+), 8 deletions(-) diff --git a/library/common/src/main/java/com/google/android/exoplayer2/MediaMetadata.java b/library/common/src/main/java/com/google/android/exoplayer2/MediaMetadata.java index 7b2cdd8794..b7775f1629 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/MediaMetadata.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/MediaMetadata.java @@ -49,6 +49,8 @@ public final class MediaMetadata implements Bundleable { @Nullable private Rating overallRating; @Nullable private byte[] artworkData; @Nullable private Uri artworkUri; + @Nullable private Integer trackNumber; + @Nullable private Integer totalTrackCount; public Builder() {} @@ -65,6 +67,8 @@ public final class MediaMetadata implements Bundleable { this.overallRating = mediaMetadata.overallRating; this.artworkData = mediaMetadata.artworkData; this.artworkUri = mediaMetadata.artworkUri; + this.trackNumber = mediaMetadata.trackNumber; + this.totalTrackCount = mediaMetadata.totalTrackCount; } /** Sets the title. */ @@ -143,6 +147,18 @@ public final class MediaMetadata implements Bundleable { return this; } + /** Sets the track number. */ + public Builder setTrackNumber(@Nullable Integer trackNumber) { + this.trackNumber = trackNumber; + return this; + } + + /** Sets the total number of tracks. */ + public Builder setTotalTrackCount(@Nullable Integer totalTrackCount) { + this.totalTrackCount = totalTrackCount; + return this; + } + /** * Sets all fields supported by the {@link Metadata.Entry entries} within the {@link Metadata}. * @@ -218,6 +234,10 @@ public final class MediaMetadata implements Bundleable { @Nullable public final byte[] artworkData; /** Optional artwork {@link Uri}. */ @Nullable public final Uri artworkUri; + /** Optional track number. */ + @Nullable public final Integer trackNumber; + /** Optional total number of tracks. */ + @Nullable public final Integer totalTrackCount; private MediaMetadata(Builder builder) { this.title = builder.title; @@ -232,6 +252,8 @@ public final class MediaMetadata implements Bundleable { this.overallRating = builder.overallRating; this.artworkData = builder.artworkData; this.artworkUri = builder.artworkUri; + this.trackNumber = builder.trackNumber; + this.totalTrackCount = builder.totalTrackCount; } /** Returns a new {@link Builder} instance with the current {@link MediaMetadata} fields. */ @@ -259,7 +281,9 @@ public final class MediaMetadata implements Bundleable { && Util.areEqual(userRating, that.userRating) && Util.areEqual(overallRating, that.overallRating) && Arrays.equals(artworkData, that.artworkData) - && Util.areEqual(artworkUri, that.artworkUri); + && Util.areEqual(artworkUri, that.artworkUri) + && Util.areEqual(trackNumber, that.trackNumber) + && Util.areEqual(totalTrackCount, that.totalTrackCount); } @Override @@ -276,7 +300,9 @@ public final class MediaMetadata implements Bundleable { userRating, overallRating, Arrays.hashCode(artworkData), - artworkUri); + artworkUri, + trackNumber, + totalTrackCount); } // Bundleable implementation. @@ -295,7 +321,9 @@ public final class MediaMetadata implements Bundleable { FIELD_USER_RATING, FIELD_OVERALL_RATING, FIELD_ARTWORK_DATA, - FIELD_ARTWORK_URI + FIELD_ARTWORK_URI, + FIELD_TRACK_NUMBER, + FIELD_TOTAL_TRACK_COUNT }) private @interface FieldNumber {} @@ -311,6 +339,8 @@ public final class MediaMetadata implements Bundleable { private static final int FIELD_OVERALL_RATING = 9; private static final int FIELD_ARTWORK_DATA = 10; private static final int FIELD_ARTWORK_URI = 11; + private static final int FIELD_TRACK_NUMBER = 12; + private static final int FIELD_TOTAL_TRACK_COUNT = 13; @Override public Bundle toBundle() { @@ -332,7 +362,12 @@ public final class MediaMetadata implements Bundleable { if (overallRating != null) { bundle.putBundle(keyForField(FIELD_OVERALL_RATING), overallRating.toBundle()); } - + if (trackNumber != null) { + bundle.putInt(keyForField(FIELD_TRACK_NUMBER), trackNumber); + } + if (totalTrackCount != null) { + bundle.putInt(keyForField(FIELD_TOTAL_TRACK_COUNT), totalTrackCount); + } return bundle; } @@ -365,6 +400,12 @@ public final class MediaMetadata implements Bundleable { builder.setOverallRating(Rating.CREATOR.fromBundle(fieldBundle)); } } + if (bundle.containsKey(keyForField(FIELD_TRACK_NUMBER))) { + builder.setTrackNumber(bundle.getInt(keyForField(FIELD_TRACK_NUMBER))); + } + if (bundle.containsKey(keyForField(FIELD_TOTAL_TRACK_COUNT))) { + builder.setTotalTrackCount(bundle.getInt(keyForField(FIELD_TOTAL_TRACK_COUNT))); + } return builder.build(); } diff --git a/library/common/src/main/java/com/google/android/exoplayer2/metadata/id3/TextInformationFrame.java b/library/common/src/main/java/com/google/android/exoplayer2/metadata/id3/TextInformationFrame.java index 4a36b7afe7..dfdcc35c2b 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/metadata/id3/TextInformationFrame.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/metadata/id3/TextInformationFrame.java @@ -60,6 +60,19 @@ public final class TextInformationFrame extends Id3Frame { case "TALB": builder.setAlbumTitle(value); break; + case "TRK": + case "TRCK": + String[] trackNumbers = Util.split(value, "/"); + try { + int trackNumber = Integer.parseInt(trackNumbers[0]); + @Nullable + Integer totalTrackCount = + trackNumbers.length > 1 ? Integer.parseInt(trackNumbers[1]) : null; + builder.setTrackNumber(trackNumber).setTotalTrackCount(totalTrackCount); + } catch (NumberFormatException e) { + // Do nothing, invalid input. + } + break; default: break; } diff --git a/library/common/src/test/java/com/google/android/exoplayer2/MediaMetadataTest.java b/library/common/src/test/java/com/google/android/exoplayer2/MediaMetadataTest.java index 4c15e2a546..80e23560c7 100644 --- a/library/common/src/test/java/com/google/android/exoplayer2/MediaMetadataTest.java +++ b/library/common/src/test/java/com/google/android/exoplayer2/MediaMetadataTest.java @@ -23,7 +23,9 @@ import com.google.android.exoplayer2.metadata.Metadata; import com.google.android.exoplayer2.metadata.id3.ApicFrame; import com.google.android.exoplayer2.metadata.id3.TextInformationFrame; import com.google.android.exoplayer2.util.MimeTypes; +import com.google.common.collect.ImmutableList; import java.util.Arrays; +import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; @@ -91,14 +93,35 @@ public class MediaMetadataTest { } @Test - public void builderPopulatedFromTextInformationFrameEntry_setsTitle() { + public void builderPopulatedFromTextInformationFrameEntry_setsValues() { String title = "the title"; - Metadata.Entry entry = - new TextInformationFrame(/* id= */ "TT2", /* description= */ null, /* value= */ title); + String artist = "artist"; + String albumTitle = "album title"; + String albumArtist = "album Artist"; + String trackNumberInfo = "11/17"; + + List entries = + ImmutableList.of( + new TextInformationFrame(/* id= */ "TT2", /* description= */ null, /* value= */ title), + new TextInformationFrame(/* id= */ "TP1", /* description= */ null, /* value= */ artist), + new TextInformationFrame( + /* id= */ "TAL", /* description= */ null, /* value= */ albumTitle), + new TextInformationFrame( + /* id= */ "TP2", /* description= */ null, /* value= */ albumArtist), + new TextInformationFrame( + /* id= */ "TRK", /* description= */ null, /* value= */ trackNumberInfo)); MediaMetadata.Builder builder = MediaMetadata.EMPTY.buildUpon(); - entry.populateMediaMetadata(builder); + for (Metadata.Entry entry : entries) { + entry.populateMediaMetadata(builder); + } + assertThat(builder.build().title.toString()).isEqualTo(title); + assertThat(builder.build().artist.toString()).isEqualTo(artist); + assertThat(builder.build().albumTitle.toString()).isEqualTo(albumTitle); + assertThat(builder.build().albumArtist.toString()).isEqualTo(albumArtist); + assertThat(builder.build().trackNumber).isEqualTo(11); + assertThat(builder.build().totalTrackCount).isEqualTo(17); } @Test