Add track number & total to MediaMetadata

#minor-release

PiperOrigin-RevId: 374235979
This commit is contained in:
samrobinson 2021-05-17 19:05:54 +01:00 committed by Oliver Woodman
parent 6fd20ddace
commit 25b453a5fe
3 changed files with 85 additions and 8 deletions

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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<Metadata.Entry> 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