使用 NotificationCompat.MediaStyle 和 MediaSessionCompat 从通知中控制我的网络广播播放器
Control my web radio player from notification with NotificationCompat.MediaStyle and MediaSessionCompat
我有一个应用程序可以从服务中复制网络广播(使用 AACDecoder 库)并在 activity 未恢复时在简单的持续通知中显示正在播放的内容。
我想要的是通过 NotificationCompat.MediaStyle 添加一个按钮来停止(并最终重新开始)通知播放,而不需要打开 activity。尽管它是一个网络广播播放器而不是一个有复制列表的播放器,我只需要一个播放暂停按钮。
但即使阅读了各种指南 (like this),我也无法正确理解和实现此功能,主要是因为最近的弃用和新的兼容性 类 在这种情况下。
我的通知是这样显示的:
这就是我想要的(大约,只需要暂停播放按钮):
欢迎任何帮助。提前致谢!
您正在寻找的更多是 MediaStyle 通知的折叠版本。虽然我不确定如何强制它保持折叠状态,但这应该会让您走上正确的道路。我使用了一些在 UniversalMusicPlayer 中找到的代码,它是为 Android.
提供的示例代码的一部分
具体来说 - 您想要查看 MediaNotificationManager 和 MusicService 类。
两者对于您要实现的目标来说都有些过于复杂,但可以作为参考的好地方。具体来说,createNotification 方法(参见下面的 Play/Pause 版本)。
private Notification createNotification() {
if (mMetadata == null || mPlaybackState == null) {
return null;
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mService);
int playPauseButtonPosition = 0;
addPlayPauseAction(notificationBuilder);
MediaDescriptionCompat description = mMetadata.getDescription();
String fetchArtUrl = null;
Bitmap art = null;
if (description.getIconUri() != null) {
// This sample assumes the iconUri will be a valid URL formatted String, but
// it can actually be any valid Android Uri formatted String.
// async fetch the album art icon
String artUrl = description.getIconUri().toString();
art = ChannelImageCache.getInstance().getBigImage(artUrl);
if (art == null) {
fetchArtUrl = artUrl;
// use a placeholder art while the remote art is being downloaded
art = BitmapFactory.decodeResource(mService.getResources(),
R.drawable.ic_default_art);
}
}
notificationBuilder
.setStyle(new NotificationCompat.MediaStyle()
.setShowActionsInCompactView(
new int[]{playPauseButtonPosition}) // show only play/pause in compact view
.setMediaSession(mSession.getSessionToken()))
.setColor(mNotificationColor)
.setSmallIcon(R.drawable.ic_notification)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setUsesChronometer(true)
.setContentIntent(createContentIntent(description))
.setContentTitle(description.getTitle())
.setContentText(description.getSubtitle())
.setLargeIcon(art);
setNotificationPlaybackState(notificationBuilder);
if (fetchArtUrl != null) {
fetchBitmapFromURLAsync(fetchArtUrl, notificationBuilder);
}
return notificationBuilder.build();
}
private void addPlayPauseAction(NotificationCompat.Builder builder) {
String label;
int icon;
PendingIntent intent;
if (mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) {
label = mService.getString(R.string.mr_media_route_controller_pause);
icon = R.drawable.uamp_ic_pause_white_24dp;
intent = mPauseIntent;
} else {
label = mService.getString(R.string.mr_media_route_controller_play);
icon = R.drawable.uamp_ic_play_arrow_white_24dp;
intent = mPlayIntent;
}
builder.addAction(new NotificationCompat.Action(icon, label, intent));
}
此通知将在您的服务中使用 MediaSession(我假设您 AACDecoder/AACPlayer 还活着)。当您从播放器获取 ID3 元数据时,您需要更新元数据并将其设置为 mediaSession。
private void updateMetadata() {
final MediaMetadataCompat track = generateMediaMetadataCompat(currentMetaData);
mediaSession.setMetadata(track);
}
private MediaMetadataCompat generateMediaMetadataCompat(SomeMetadataModelObject streamMetadata)
{
currentMediaMetadataCompat = new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, streamMetadata.getMediaId())
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, streamMetadata.getAlbumName())
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, streamMetadata.getArtistName())
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI, "http://somealbumart.png" )
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, streamMetadata.getSongTitle())
.build();
return currentMediaMetadataCompat;
}
我有一个应用程序可以从服务中复制网络广播(使用 AACDecoder 库)并在 activity 未恢复时在简单的持续通知中显示正在播放的内容。 我想要的是通过 NotificationCompat.MediaStyle 添加一个按钮来停止(并最终重新开始)通知播放,而不需要打开 activity。尽管它是一个网络广播播放器而不是一个有复制列表的播放器,我只需要一个播放暂停按钮。 但即使阅读了各种指南 (like this),我也无法正确理解和实现此功能,主要是因为最近的弃用和新的兼容性 类 在这种情况下。
我的通知是这样显示的:
这就是我想要的(大约,只需要暂停播放按钮):
欢迎任何帮助。提前致谢!
您正在寻找的更多是 MediaStyle 通知的折叠版本。虽然我不确定如何强制它保持折叠状态,但这应该会让您走上正确的道路。我使用了一些在 UniversalMusicPlayer 中找到的代码,它是为 Android.
提供的示例代码的一部分具体来说 - 您想要查看 MediaNotificationManager 和 MusicService 类。
两者对于您要实现的目标来说都有些过于复杂,但可以作为参考的好地方。具体来说,createNotification 方法(参见下面的 Play/Pause 版本)。
private Notification createNotification() {
if (mMetadata == null || mPlaybackState == null) {
return null;
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mService);
int playPauseButtonPosition = 0;
addPlayPauseAction(notificationBuilder);
MediaDescriptionCompat description = mMetadata.getDescription();
String fetchArtUrl = null;
Bitmap art = null;
if (description.getIconUri() != null) {
// This sample assumes the iconUri will be a valid URL formatted String, but
// it can actually be any valid Android Uri formatted String.
// async fetch the album art icon
String artUrl = description.getIconUri().toString();
art = ChannelImageCache.getInstance().getBigImage(artUrl);
if (art == null) {
fetchArtUrl = artUrl;
// use a placeholder art while the remote art is being downloaded
art = BitmapFactory.decodeResource(mService.getResources(),
R.drawable.ic_default_art);
}
}
notificationBuilder
.setStyle(new NotificationCompat.MediaStyle()
.setShowActionsInCompactView(
new int[]{playPauseButtonPosition}) // show only play/pause in compact view
.setMediaSession(mSession.getSessionToken()))
.setColor(mNotificationColor)
.setSmallIcon(R.drawable.ic_notification)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setUsesChronometer(true)
.setContentIntent(createContentIntent(description))
.setContentTitle(description.getTitle())
.setContentText(description.getSubtitle())
.setLargeIcon(art);
setNotificationPlaybackState(notificationBuilder);
if (fetchArtUrl != null) {
fetchBitmapFromURLAsync(fetchArtUrl, notificationBuilder);
}
return notificationBuilder.build();
}
private void addPlayPauseAction(NotificationCompat.Builder builder) {
String label;
int icon;
PendingIntent intent;
if (mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) {
label = mService.getString(R.string.mr_media_route_controller_pause);
icon = R.drawable.uamp_ic_pause_white_24dp;
intent = mPauseIntent;
} else {
label = mService.getString(R.string.mr_media_route_controller_play);
icon = R.drawable.uamp_ic_play_arrow_white_24dp;
intent = mPlayIntent;
}
builder.addAction(new NotificationCompat.Action(icon, label, intent));
}
此通知将在您的服务中使用 MediaSession(我假设您 AACDecoder/AACPlayer 还活着)。当您从播放器获取 ID3 元数据时,您需要更新元数据并将其设置为 mediaSession。
private void updateMetadata() {
final MediaMetadataCompat track = generateMediaMetadataCompat(currentMetaData);
mediaSession.setMetadata(track);
}
private MediaMetadataCompat generateMediaMetadataCompat(SomeMetadataModelObject streamMetadata)
{
currentMediaMetadataCompat = new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, streamMetadata.getMediaId())
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, streamMetadata.getAlbumName())
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, streamMetadata.getArtistName())
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI, "http://somealbumart.png" )
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, streamMetadata.getSongTitle())
.build();
return currentMediaMetadataCompat;
}