mirror of
https://github.com/androidx/media.git
synced 2025-05-06 23:20:42 +08:00
Set StyledPlayerView/PlayerView artwork from MediaMetadata.
PiperOrigin-RevId: 385763366
This commit is contained in:
parent
c157db694a
commit
0b0277af50
@ -49,15 +49,13 @@ import com.google.android.exoplayer2.C;
|
||||
import com.google.android.exoplayer2.ControlDispatcher;
|
||||
import com.google.android.exoplayer2.Format;
|
||||
import com.google.android.exoplayer2.ForwardingPlayer;
|
||||
import com.google.android.exoplayer2.MediaMetadata;
|
||||
import com.google.android.exoplayer2.PlaybackException;
|
||||
import com.google.android.exoplayer2.Player;
|
||||
import com.google.android.exoplayer2.Player.DiscontinuityReason;
|
||||
import com.google.android.exoplayer2.SimpleExoPlayer;
|
||||
import com.google.android.exoplayer2.Timeline;
|
||||
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.text.Cue;
|
||||
import com.google.android.exoplayer2.trackselection.TrackSelection;
|
||||
@ -1288,11 +1286,9 @@ public class PlayerView extends FrameLayout implements AdViewProvider {
|
||||
closeShutter();
|
||||
// Display artwork if enabled and available, else hide it.
|
||||
if (useArtwork()) {
|
||||
for (Metadata metadata : player.getCurrentStaticMetadata()) {
|
||||
if (setArtworkFromMetadata(metadata)) {
|
||||
if (setArtworkFromMediaMetadata(player.getMediaMetadata())) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (setDrawableArtwork(defaultArtwork)) {
|
||||
return;
|
||||
}
|
||||
@ -1334,33 +1330,14 @@ public class PlayerView extends FrameLayout implements AdViewProvider {
|
||||
}
|
||||
|
||||
@RequiresNonNull("artworkView")
|
||||
private boolean setArtworkFromMetadata(Metadata metadata) {
|
||||
boolean isArtworkSet = false;
|
||||
int currentPictureType = PICTURE_TYPE_NOT_SET;
|
||||
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;
|
||||
private boolean setArtworkFromMediaMetadata(MediaMetadata mediaMetadata) {
|
||||
if (mediaMetadata.artworkData == null) {
|
||||
return false;
|
||||
}
|
||||
// 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")
|
||||
|
@ -50,15 +50,13 @@ import com.google.android.exoplayer2.C;
|
||||
import com.google.android.exoplayer2.ControlDispatcher;
|
||||
import com.google.android.exoplayer2.Format;
|
||||
import com.google.android.exoplayer2.ForwardingPlayer;
|
||||
import com.google.android.exoplayer2.MediaMetadata;
|
||||
import com.google.android.exoplayer2.PlaybackException;
|
||||
import com.google.android.exoplayer2.Player;
|
||||
import com.google.android.exoplayer2.Player.DiscontinuityReason;
|
||||
import com.google.android.exoplayer2.SimpleExoPlayer;
|
||||
import com.google.android.exoplayer2.Timeline;
|
||||
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.text.Cue;
|
||||
import com.google.android.exoplayer2.trackselection.TrackSelection;
|
||||
@ -1329,11 +1327,9 @@ public class StyledPlayerView extends FrameLayout implements AdViewProvider {
|
||||
closeShutter();
|
||||
// Display artwork if enabled and available, else hide it.
|
||||
if (useArtwork()) {
|
||||
for (Metadata metadata : player.getCurrentStaticMetadata()) {
|
||||
if (setArtworkFromMetadata(metadata)) {
|
||||
if (setArtworkFromMediaMetadata(player.getMediaMetadata())) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (setDrawableArtwork(defaultArtwork)) {
|
||||
return;
|
||||
}
|
||||
@ -1343,33 +1339,14 @@ public class StyledPlayerView extends FrameLayout implements AdViewProvider {
|
||||
}
|
||||
|
||||
@RequiresNonNull("artworkView")
|
||||
private boolean setArtworkFromMetadata(Metadata metadata) {
|
||||
boolean isArtworkSet = false;
|
||||
int currentPictureType = PICTURE_TYPE_NOT_SET;
|
||||
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;
|
||||
private boolean setArtworkFromMediaMetadata(MediaMetadata mediaMetadata) {
|
||||
if (mediaMetadata.artworkData == null) {
|
||||
return false;
|
||||
}
|
||||
// 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")
|
||||
|
Loading…
x
Reference in New Issue
Block a user