Add default artwork support to SimpleExoPlayerView

Add support for a default artwork image that is displayed if no artwork can be found in the metadata.
This commit is contained in:
meteoorkip 2016-12-31 22:48:14 +01:00
parent 163a3a7bb8
commit eda393ba82
2 changed files with 54 additions and 11 deletions

View File

@ -65,6 +65,13 @@ import java.util.List;
* <li>Default: {@code true}</li> * <li>Default: {@code true}</li>
* </ul> * </ul>
* </li> * </li>
* <li><b>{@code default_artwork}</b> - Default artwork to use if no artwork available in audio
* streams.
* <ul>
* <li>Corresponding method: {@link #setDefaultArtwork(Bitmap)}</li>
* <li>Default: {@code null}</li>
* </ul>
* </li>
* <li><b>{@code use_controller}</b> - Whether playback controls are displayed. * <li><b>{@code use_controller}</b> - Whether playback controls are displayed.
* <ul> * <ul>
* <li>Corresponding method: {@link #setUseController(boolean)}</li> * <li>Corresponding method: {@link #setUseController(boolean)}</li>
@ -179,6 +186,7 @@ public final class SimpleExoPlayerView extends FrameLayout {
private SimpleExoPlayer player; private SimpleExoPlayer player;
private boolean useController; private boolean useController;
private boolean useArtwork; private boolean useArtwork;
private Bitmap defaultArtwork;
private int controllerShowTimeoutMs; private int controllerShowTimeoutMs;
public SimpleExoPlayerView(Context context) { public SimpleExoPlayerView(Context context) {
@ -194,6 +202,7 @@ public final class SimpleExoPlayerView extends FrameLayout {
int playerLayoutId = R.layout.exo_simple_player_view; int playerLayoutId = R.layout.exo_simple_player_view;
boolean useArtwork = true; boolean useArtwork = true;
int defaultArtwork = 0;
boolean useController = true; boolean useController = true;
int surfaceType = SURFACE_TYPE_SURFACE_VIEW; int surfaceType = SURFACE_TYPE_SURFACE_VIEW;
int resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT; int resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT;
@ -205,6 +214,8 @@ public final class SimpleExoPlayerView extends FrameLayout {
playerLayoutId = a.getResourceId(R.styleable.SimpleExoPlayerView_player_layout_id, playerLayoutId = a.getResourceId(R.styleable.SimpleExoPlayerView_player_layout_id,
playerLayoutId); playerLayoutId);
useArtwork = a.getBoolean(R.styleable.SimpleExoPlayerView_use_artwork, useArtwork); useArtwork = a.getBoolean(R.styleable.SimpleExoPlayerView_use_artwork, useArtwork);
defaultArtwork = a.getResourceId(R.styleable.SimpleExoPlayerView_default_artwork,
defaultArtwork);
useController = a.getBoolean(R.styleable.SimpleExoPlayerView_use_controller, useController); useController = a.getBoolean(R.styleable.SimpleExoPlayerView_use_controller, useController);
surfaceType = a.getInt(R.styleable.SimpleExoPlayerView_surface_type, surfaceType); surfaceType = a.getInt(R.styleable.SimpleExoPlayerView_surface_type, surfaceType);
resizeMode = a.getInt(R.styleable.SimpleExoPlayerView_resize_mode, resizeMode); resizeMode = a.getInt(R.styleable.SimpleExoPlayerView_resize_mode, resizeMode);
@ -246,6 +257,9 @@ public final class SimpleExoPlayerView extends FrameLayout {
// Artwork view. // Artwork view.
artworkView = (ImageView) findViewById(R.id.exo_artwork); artworkView = (ImageView) findViewById(R.id.exo_artwork);
this.useArtwork = useArtwork && artworkView != null; this.useArtwork = useArtwork && artworkView != null;
if (defaultArtwork != 0) {
this.defaultArtwork = BitmapFactory.decodeResource(context.getResources(), defaultArtwork);
}
// Subtitle view. // Subtitle view.
subtitleView = (SubtitleView) findViewById(R.id.exo_subtitles); subtitleView = (SubtitleView) findViewById(R.id.exo_subtitles);
@ -351,6 +365,26 @@ public final class SimpleExoPlayerView extends FrameLayout {
} }
} }
/**
* Returns the default artwork to display.
*/
public Bitmap getDefaultArtwork() {
return defaultArtwork;
}
/**
* Sets the default artwork to display if {@code useArtwork} is {@code true} and no artwork is
* present in the media.
*
* @param defaultArtwork the default artwork to display.
*/
public void setDefaultArtwork(Bitmap defaultArtwork) {
if (this.defaultArtwork != defaultArtwork) {
this.defaultArtwork = defaultArtwork;
updateForCurrentTrackSelections();
}
}
/** /**
* Returns whether the playback controls are enabled. * Returns whether the playback controls are enabled.
*/ */
@ -569,6 +603,9 @@ public final class SimpleExoPlayerView extends FrameLayout {
} }
} }
} }
if (setArtworkFromBitmap(defaultArtwork)) {
return;
}
} }
// Artwork disabled or unavailable. // Artwork disabled or unavailable.
hideArtwork(); hideArtwork();
@ -580,6 +617,13 @@ public final class SimpleExoPlayerView extends FrameLayout {
if (metadataEntry instanceof ApicFrame) { if (metadataEntry instanceof ApicFrame) {
byte[] bitmapData = ((ApicFrame) metadataEntry).pictureData; byte[] bitmapData = ((ApicFrame) metadataEntry).pictureData;
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length); Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length);
return setArtworkFromBitmap(bitmap);
}
}
return false;
}
private boolean setArtworkFromBitmap(Bitmap bitmap) {
if (bitmap != null) { if (bitmap != null) {
int bitmapWidth = bitmap.getWidth(); int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight(); int bitmapHeight = bitmap.getHeight();
@ -592,8 +636,6 @@ public final class SimpleExoPlayerView extends FrameLayout {
return true; return true;
} }
} }
}
}
return false; return false;
} }

View File

@ -37,6 +37,7 @@
<declare-styleable name="SimpleExoPlayerView"> <declare-styleable name="SimpleExoPlayerView">
<attr name="use_artwork" format="boolean"/> <attr name="use_artwork" format="boolean"/>
<attr name="default_artwork" format="reference"/>
<attr name="use_controller" format="boolean"/> <attr name="use_controller" format="boolean"/>
<attr name="surface_type"/> <attr name="surface_type"/>
<attr name="show_timeout"/> <attr name="show_timeout"/>