Media3 ExoPlayer 在播放期间更新通知标题

问题描述 投票:0回答:1

我正在使用 Media3 ExoPlayer@1.2.0 以及扩展

MediaSessionService
PlaybackService

使用

setMediaNotificationProvider
为低于 33 的 API 创建
Notification

public class PlaybackService extends MediaSessionService {

    public void onCreate() {

        // Initialize ExoPlayer
        ExoPlayer player = new ExoPlayer.Builder(this, ...).build();

        setMediaNotificationProvider(new MediaNotification.Provider() {
            @NonNull
            @Override
            public MediaNotification createNotification(@NonNull MediaSession mediaSession, @NonNull ImmutableList<CommandButton> customLayout, @NonNull MediaNotification.ActionFactory actionFactory, @NonNull Callback onNotificationChangedCallback) {

                createMediaNotification(mediaSession);

                return new MediaNotification(PLAYBACK_NOTIFICATION_ID, notificationBuilder.build());
            }

            // ...
        });

        // ...

    });

    public void createMediaNotification(MediaSession mediaSession) {

        MediaMetadata metadata = mediaSession.getPlayer().getMediaMetadata();

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(PlaybackService.this, PLAYBACK_CHANNEL_ID)
            // ...
            .setContentTitle(metadata.title)
            .setContentText(metadata.description)
            .setStyle(new MediaStyleNotificationHelper.MediaStyle(mediaSession));

    }

}

MainActivity
中我设置了
MediaMetaData
(标题/描述)

List<MediaItem> mediaItems = new ArrayList<>();

MediaItem mediaItem = new MediaItem.Builder()
    .setMediaId(ITEM_ID)
    .setMediaMetadata(new MediaMetadata.Builder()
            .setTitle(ITEM_TITLE)
            .setDescription(ITEM_DESCRIPTION)
            // ..
    .setUri(ITEM_URI)
.build();

mediaItems.add(mediaItem);

player.setMediaItems(mediaItems);

player.prepare();
player.play();

TLDR: 我需要在使用

.setMediaMetadata(new MediaMetadata.Builder().setTitle(..))
.setContentTitle(metadata.title)

之前设置媒体播放通知的标题/描述(在播放期间)

现在,我想要实现的目标是,在某个时刻

(during playback)
我需要更新当前播放媒体项目的通知的标题/描述(或 MediaMetaData 标题/描述),无论它是
Media3 Session Notification
Legacy Notification
API 级别 33 以下

// a method in MainActivity
private void onPlayerProgressChanged(long current_playback_time) {

    if(current_playback_time >= something) {
        
        // Here need to update the notification title/description
        // Like .setDescription("Something else")
    }

}

目前的运作方式如下:

我似乎找不到任何文档或解决方法来实现这种方法,不知道它是否可能。

我怎样才能实现这个目标?

java android exoplayer android-media3 mediasession
1个回答
0
投票

我设法在播放过程中通过调用 title 来更新

MediaMetaData
player.replaceMediaItem(..)
,如此处

所述
MediaItem oldItem = player.getCurrentMediaItem();

if(oldItem != null) {
    MediaItem newItem = oldItem
        .buildUpon()
        .setMediaMetadata(player.getMediaMetadata()
            .buildUpon()
            .setTitle("New Title")
            .build())
        .build();
    player.replaceMediaItem(player.getCurrentMediaItemIndex(), newItem);
}

通过这种方式,玩家可以发出 EVENT_MEDIA_METADATA_CHANGED 事件和 media3 会话 MediaNotificationManager 或旧版 PlayerNotificationManager 可以在收到通知时更新通知 元数据更改事件。

© www.soinside.com 2019 - 2024. All rights reserved.