Set StyledPlayerView/PlayerView artwork from MediaMetadata.

PiperOrigin-RevId: 385763366
This commit is contained in:
samrobinson 2021-07-20 13:14:09 +01:00 committed by Ian Baker
parent c157db694a
commit 0b0277af50
2 changed files with 20 additions and 66 deletions

View File

@ -49,15 +49,13 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ControlDispatcher; import com.google.android.exoplayer2.ControlDispatcher;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.ForwardingPlayer; import com.google.android.exoplayer2.ForwardingPlayer;
import com.google.android.exoplayer2.MediaMetadata;
import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.PlaybackException;
import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.Player.DiscontinuityReason; import com.google.android.exoplayer2.Player.DiscontinuityReason;
import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.Timeline.Period; import com.google.android.exoplayer2.Timeline.Period;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.metadata.flac.PictureFrame;
import com.google.android.exoplayer2.metadata.id3.ApicFrame;
import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.text.Cue; import com.google.android.exoplayer2.text.Cue;
import com.google.android.exoplayer2.trackselection.TrackSelection; import com.google.android.exoplayer2.trackselection.TrackSelection;
@ -1288,10 +1286,8 @@ public class PlayerView extends FrameLayout implements AdViewProvider {
closeShutter(); closeShutter();
// Display artwork if enabled and available, else hide it. // Display artwork if enabled and available, else hide it.
if (useArtwork()) { if (useArtwork()) {
for (Metadata metadata : player.getCurrentStaticMetadata()) { if (setArtworkFromMediaMetadata(player.getMediaMetadata())) {
if (setArtworkFromMetadata(metadata)) { return;
return;
}
} }
if (setDrawableArtwork(defaultArtwork)) { if (setDrawableArtwork(defaultArtwork)) {
return; return;
@ -1334,33 +1330,14 @@ public class PlayerView extends FrameLayout implements AdViewProvider {
} }
@RequiresNonNull("artworkView") @RequiresNonNull("artworkView")
private boolean setArtworkFromMetadata(Metadata metadata) { private boolean setArtworkFromMediaMetadata(MediaMetadata mediaMetadata) {
boolean isArtworkSet = false; if (mediaMetadata.artworkData == null) {
int currentPictureType = PICTURE_TYPE_NOT_SET; return false;
for (int i = 0; i < metadata.length(); i++) {
Metadata.Entry metadataEntry = metadata.get(i);
int pictureType;
byte[] bitmapData;
if (metadataEntry instanceof ApicFrame) {
bitmapData = ((ApicFrame) metadataEntry).pictureData;
pictureType = ((ApicFrame) metadataEntry).pictureType;
} else if (metadataEntry instanceof PictureFrame) {
bitmapData = ((PictureFrame) metadataEntry).pictureData;
pictureType = ((PictureFrame) metadataEntry).pictureType;
} else {
continue;
}
// Prefer the first front cover picture. If there aren't any, prefer the first picture.
if (currentPictureType == PICTURE_TYPE_NOT_SET || pictureType == PICTURE_TYPE_FRONT_COVER) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length);
isArtworkSet = setDrawableArtwork(new BitmapDrawable(getResources(), bitmap));
currentPictureType = pictureType;
if (currentPictureType == PICTURE_TYPE_FRONT_COVER) {
break;
}
}
} }
return isArtworkSet; Bitmap bitmap =
BitmapFactory.decodeByteArray(
mediaMetadata.artworkData, /* offset= */ 0, mediaMetadata.artworkData.length);
return setDrawableArtwork(new BitmapDrawable(getResources(), bitmap));
} }
@RequiresNonNull("artworkView") @RequiresNonNull("artworkView")

View File

@ -50,15 +50,13 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ControlDispatcher; import com.google.android.exoplayer2.ControlDispatcher;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.ForwardingPlayer; import com.google.android.exoplayer2.ForwardingPlayer;
import com.google.android.exoplayer2.MediaMetadata;
import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.PlaybackException;
import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.Player.DiscontinuityReason; import com.google.android.exoplayer2.Player.DiscontinuityReason;
import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.Timeline.Period; import com.google.android.exoplayer2.Timeline.Period;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.metadata.flac.PictureFrame;
import com.google.android.exoplayer2.metadata.id3.ApicFrame;
import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.text.Cue; import com.google.android.exoplayer2.text.Cue;
import com.google.android.exoplayer2.trackselection.TrackSelection; import com.google.android.exoplayer2.trackselection.TrackSelection;
@ -1329,10 +1327,8 @@ public class StyledPlayerView extends FrameLayout implements AdViewProvider {
closeShutter(); closeShutter();
// Display artwork if enabled and available, else hide it. // Display artwork if enabled and available, else hide it.
if (useArtwork()) { if (useArtwork()) {
for (Metadata metadata : player.getCurrentStaticMetadata()) { if (setArtworkFromMediaMetadata(player.getMediaMetadata())) {
if (setArtworkFromMetadata(metadata)) { return;
return;
}
} }
if (setDrawableArtwork(defaultArtwork)) { if (setDrawableArtwork(defaultArtwork)) {
return; return;
@ -1343,33 +1339,14 @@ public class StyledPlayerView extends FrameLayout implements AdViewProvider {
} }
@RequiresNonNull("artworkView") @RequiresNonNull("artworkView")
private boolean setArtworkFromMetadata(Metadata metadata) { private boolean setArtworkFromMediaMetadata(MediaMetadata mediaMetadata) {
boolean isArtworkSet = false; if (mediaMetadata.artworkData == null) {
int currentPictureType = PICTURE_TYPE_NOT_SET; return false;
for (int i = 0; i < metadata.length(); i++) {
Metadata.Entry metadataEntry = metadata.get(i);
int pictureType;
byte[] bitmapData;
if (metadataEntry instanceof ApicFrame) {
bitmapData = ((ApicFrame) metadataEntry).pictureData;
pictureType = ((ApicFrame) metadataEntry).pictureType;
} else if (metadataEntry instanceof PictureFrame) {
bitmapData = ((PictureFrame) metadataEntry).pictureData;
pictureType = ((PictureFrame) metadataEntry).pictureType;
} else {
continue;
}
// Prefer the first front cover picture. If there aren't any, prefer the first picture.
if (currentPictureType == PICTURE_TYPE_NOT_SET || pictureType == PICTURE_TYPE_FRONT_COVER) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length);
isArtworkSet = setDrawableArtwork(new BitmapDrawable(getResources(), bitmap));
currentPictureType = pictureType;
if (currentPictureType == PICTURE_TYPE_FRONT_COVER) {
break;
}
}
} }
return isArtworkSet; Bitmap bitmap =
BitmapFactory.decodeByteArray(
mediaMetadata.artworkData, /* offset= */ 0, mediaMetadata.artworkData.length);
return setDrawableArtwork(new BitmapDrawable(getResources(), bitmap));
} }
@RequiresNonNull("artworkView") @RequiresNonNull("artworkView")