Skip to content

Commit

Permalink
Merge pull request #2273 from meteoorkip/default_artwork
Browse files Browse the repository at this point in the history
Add default artwork support to SimpleExoPlayerView
  • Loading branch information
ojw28 authored Jan 3, 2017
2 parents 163a3a7 + eda393b commit 1d8302c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@
* <li>Default: {@code true}</li>
* </ul>
* </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.
* <ul>
* <li>Corresponding method: {@link #setUseController(boolean)}</li>
Expand Down Expand Up @@ -179,6 +186,7 @@ public final class SimpleExoPlayerView extends FrameLayout {
private SimpleExoPlayer player;
private boolean useController;
private boolean useArtwork;
private Bitmap defaultArtwork;
private int controllerShowTimeoutMs;

public SimpleExoPlayerView(Context context) {
Expand All @@ -194,6 +202,7 @@ public SimpleExoPlayerView(Context context, AttributeSet attrs, int defStyleAttr

int playerLayoutId = R.layout.exo_simple_player_view;
boolean useArtwork = true;
int defaultArtwork = 0;
boolean useController = true;
int surfaceType = SURFACE_TYPE_SURFACE_VIEW;
int resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT;
Expand All @@ -205,6 +214,8 @@ public SimpleExoPlayerView(Context context, AttributeSet attrs, int defStyleAttr
playerLayoutId = a.getResourceId(R.styleable.SimpleExoPlayerView_player_layout_id,
playerLayoutId);
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);
surfaceType = a.getInt(R.styleable.SimpleExoPlayerView_surface_type, surfaceType);
resizeMode = a.getInt(R.styleable.SimpleExoPlayerView_resize_mode, resizeMode);
Expand Down Expand Up @@ -246,6 +257,9 @@ public SimpleExoPlayerView(Context context, AttributeSet attrs, int defStyleAttr
// Artwork view.
artworkView = (ImageView) findViewById(R.id.exo_artwork);
this.useArtwork = useArtwork && artworkView != null;
if (defaultArtwork != 0) {
this.defaultArtwork = BitmapFactory.decodeResource(context.getResources(), defaultArtwork);
}

// Subtitle view.
subtitleView = (SubtitleView) findViewById(R.id.exo_subtitles);
Expand Down Expand Up @@ -351,6 +365,26 @@ public void setUseArtwork(boolean useArtwork) {
}
}

/**
* 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.
*/
Expand Down Expand Up @@ -569,6 +603,9 @@ private void updateForCurrentTrackSelections() {
}
}
}
if (setArtworkFromBitmap(defaultArtwork)) {
return;
}
}
// Artwork disabled or unavailable.
hideArtwork();
Expand All @@ -580,18 +617,23 @@ private boolean setArtworkFromMetadata(Metadata metadata) {
if (metadataEntry instanceof ApicFrame) {
byte[] bitmapData = ((ApicFrame) metadataEntry).pictureData;
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length);
if (bitmap != null) {
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
if (bitmapWidth > 0 && bitmapHeight > 0) {
if (contentFrame != null) {
contentFrame.setAspectRatio((float) bitmapWidth / bitmapHeight);
}
artworkView.setImageBitmap(bitmap);
artworkView.setVisibility(VISIBLE);
return true;
}
return setArtworkFromBitmap(bitmap);
}
}
return false;
}

private boolean setArtworkFromBitmap(Bitmap bitmap) {
if (bitmap != null) {
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
if (bitmapWidth > 0 && bitmapHeight > 0) {
if (contentFrame != null) {
contentFrame.setAspectRatio((float) bitmapWidth / bitmapHeight);
}
artworkView.setImageBitmap(bitmap);
artworkView.setVisibility(VISIBLE);
return true;
}
}
return false;
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

<declare-styleable name="SimpleExoPlayerView">
<attr name="use_artwork" format="boolean"/>
<attr name="default_artwork" format="reference"/>
<attr name="use_controller" format="boolean"/>
<attr name="surface_type"/>
<attr name="show_timeout"/>
Expand Down

0 comments on commit 1d8302c

Please sign in to comment.