问题
I need help about displaying the Now Playing Card in the Recommendations row. I read the post about it in the Android developers site, but that did not help me much.
I have a service that streams the MP3 data without problems. I added the following code, but there's no Now Playing Card...
@Override
public void onCreate() {
Log.i(TAG, "onCreate called");
mSession = new MediaSession(this, "MusicService");
mSession.setCallback(new MediaSessionCallback());
mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
mSession.setActive(true);
}
private class MediaSessionCallback extends MediaSession.Callback {
}
Edit: Added code for meta-data:
@Override
public void onCreate() {
Log.i(TAG, "onCreate called");
mSession = new MediaSession(this, "MusicService");
mSession.setCallback(new MediaSessionCallback());
mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
final MediaMetadata.Builder metadataBuilder = new MediaMetadata.Builder();
String title = "Burak";
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE, title);
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE,
"Burak müzik");
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI,
"http://commondatastorage.googleapis.com/android-tv/Sample%20videos/Zeitgeist/Zeitgeist%202010_%20Year%20in%20Review/card.jpg");
// And at minimum the title and artist for legacy support
metadataBuilder.putString(MediaMetadata.METADATA_KEY_TITLE, title);
metadataBuilder.putString(MediaMetadata.METADATA_KEY_ARTIST, "BKD Mobile");
mSession.setMetadata(metadataBuilder.build());
mSession.setActive(true);
}
回答1:
I would say you need to specify the metadata for the current playing MP3 file via the Metadata.Builder class:
MediaMetadata.Builder metadataBuilder = new MediaMetadata.Builder();
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE,
"Title");
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE,
"Subtitle");
metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI,
"Artwork");
mSession.setMetadata(metadataBuilder.build());
回答2:
This is probably too late to help the OP, but in the event someone runs across this page, I wanted to share a possible answer. You should ensure that you are calling mSession.setPlayBackState(state) at some point. For instance, try doing something like this to start:
mediaSession.setMetadata(builder.build());
PlaybackState state = new PlaybackState.Builder()
.setActions(
PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PAUSE | PlaybackState
.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_REWIND |
PlaybackState.ACTION_FAST_FORWARD)
.setState(PlaybackState.STATE_NONE, 0, 1.0f)
.build();
mediaSession.setPlaybackState(state);
You'll need to adjust the actions based on your needs. My guess is that you are setting state later, which is why you notice it working when returning to the Home screen on the subsequent attempt.
来源:https://stackoverflow.com/questions/29945306/now-playing-card