通过NotificationCompat.MediaStyle和MediaSessionCompat

时间:2015-08-05 15:02:23

标签: android android-notifications android-music-player

我有一个应用程序可以从服务中复制webradio(使用AACDecoder lib)并在简单的持续通知中显示活动未恢复时正在播放的内容。 我想要的是通过NotificationCompat.MediaStyle添加一个按钮来停止(并最终重新启动)通知播放,而无需打开活动。尽管它是一个webradio播放器而不是具有再现列表的播放器,但我只需要一个播放暂停按钮。 但即使在阅读了各种指南(like this)之后,我也无法正确理解和实现此功能,主要是因为此方案中最近的弃用和新兼容性类。

这是我的通知显示方式:

这就是我想要的(大约需要暂停播放按钮): enter image description here

欢迎任何帮助。提前致谢!

1 个答案:

答案 0 :(得分:1)

您正在寻找的是MediaStyle通知的折叠版本。虽然我不确定如何强迫它保持倒塌,但这应该让你走上正确的道路。我使用了UniversalMusicPlayer中的一些代码,它是为Android提供的示例代码的一部分。

具体 - 您想要查看MediaNotificationManager和MusicService类。

https://github.com/googlesamples/android-UniversalMusicPlayer/blob/master/mobile/src/main/java/com/example/android/uamp/MediaNotificationManager.java

对于你想要实现的目标,这两者都有点过于复杂,但可能是一个很好的参考地方。具体来说,createNotification方法(请参阅下面的播放/暂停版本)。

if($model->save())
{
     // redirect to view
}
else
{
    // check whether there are any errors
    echo '<pre>'; print_r($model->getErrors());
}

此通知将在您的服务中使用MediaSession(我假设您正在使用AACDecoder / AACPlayer)。当您从播放器获取ID3元数据时,您将要更新元数据并将其设置为mediaSession。

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));
}