How do we go about showing the lockscreen artwork for a chromecast sender
app running v2.7.0
. I have spent the best part of about 2 days on this without any resolution.
The v2.7.0
library currently has the following method in the VideoCastManager.java
class:
private void setBitmapForLockScreen(MediaInfo video) {
if (video == null || mMediaSessionCompat == null) {
return;
}
Uri imgUrl = null;
Bitmap bm = null;
List<WebImage> images = video.getMetadata().getImages();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (images.size() > 1) {
imgUrl = images.get(1).getUrl();
} else if (images.size() == 1) {
imgUrl = images.get(0).getUrl();
} else if (mContext != null) {
// we don't have a url for image so get a placeholder image from resources
bm = BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.album_art_placeholder_large);
}
} else if (!images.isEmpty()) {
imgUrl = images.get(0).getUrl();
} else {
// we don't have a url for image so get a placeholder image from resources
bm = BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.album_art_placeholder);
}
if (bm != null) {
MediaMetadataCompat currentMetadata = mMediaSessionCompat.getController().getMetadata();
MediaMetadataCompat.Builder newBuilder = currentMetadata == null
? new MediaMetadataCompat.Builder()
: new MediaMetadataCompat.Builder(currentMetadata);
mMediaSessionCompat.setMetadata(newBuilder
.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, bm)
.build());
} else {
if (mLockScreenFetchTask != null) {
mLockScreenFetchTask.cancel(true);
}
mLockScreenFetchTask = new FetchBitmapTask() {
@Override
protected void onPostExecute(Bitmap bitmap) {
if (mMediaSessionCompat != null) {
MediaMetadataCompat currentMetadata = mMediaSessionCompat.getController()
.getMetadata();
MediaMetadataCompat.Builder newBuilder = currentMetadata == null
? new MediaMetadataCompat.Builder()
: new MediaMetadataCompat.Builder(currentMetadata);
mMediaSessionCompat.setMetadata(newBuilder
.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, bitmap)
.build());
}
mLockScreenFetchTask = null;
}
};
mLockScreenFetchTask.execute(imgUrl);
}
}
I have tried swaping the album_art_placeholder_large
drawable with my own custom image, no results. Also tried adding a bitmap via the following line:
putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bm)
but nothing works. What exactly am i missing here? What MediaMetadataCompat
key needs to be used to show the lockscreen artwork??
Or i am looking at the wrong place to begin with?
Some other links i have tried but to no avail:
Android MediaMetadata image on lockscreen is zoomed
Adding more fields in MediaMetada GoogleCast
The document online as of now is incredibly poor and does not help much.
Thanks!
EDIT: On further investigation, it seems to be taking 15-20 minutes after locking the device to show the lockscreen image. Not sure why.
EDIT 2: setupMediaSession method.
private void setUpMediaSession(final MediaInfo info) {
if (!isFeatureEnabled(CastConfiguration.FEATURE_LOCKSCREEN)) {
return;
}
if (mMediaSessionCompat == null) {
ComponentName mediaEventReceiver = new ComponentName(mContext,
VideoIntentReceiver.class.getName());
mMediaSessionCompat = new MediaSessionCompat(mContext, "TAG", mediaEventReceiver,
null);
mMediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
| MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mMediaSessionCompat.setActive(true);
mMediaSessionCompat.setCallback(new MediaSessionCompat.Callback() {
@Override
public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
KeyEvent keyEvent = mediaButtonIntent
.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (keyEvent != null && (keyEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PAUSE
|| keyEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY)) {
toggle();
}
return true;
}
@Override
public void onPlay() {
toggle();
}
@Override
public void onPause() {
toggle();
}
private void toggle() {
try {
togglePlayback();
} catch (CastException | TransientNetworkDisconnectionException |
NoConnectionException e) {
LOGE(TAG, "MediaSessionCompat.Callback(): Failed to toggle playback", e);
}
}
});
}
mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
PendingIntent pi = getCastControllerPendingIntent();
if (pi != null) {
mMediaSessionCompat.setSessionActivity(pi);
}
if (info == null) {
mMediaSessionCompat.setPlaybackState(new PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_NONE, 0, 1.0f).build());
} else {
mMediaSessionCompat.setPlaybackState(new PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_PLAYING, 0, 1.0f)
.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE).build());
}
// Update the media session's image
updateLockScreenImage(info);
// update the media session's metadata
updateMediaSessionMetadata();
mMediaRouter.setMediaSessionCompat(mMediaSessionCompat);
}
Have you defined a "larger" album arts when you defined your MediaInfo? CCL assumes you have a smaller one, set as index=0, which will be used in notification and mini-controller and media router controller dialog and a larger one (second image in MediaInfo, index=1) that will be used for the lockscreen and the fullscreen remote controller. If these images are there, CCL automatically uses them. As a test, run CastVideos-android (v2) and see if that works for you.