如何修改Media3自动生成的媒体通知(Java实现)
自定义Media3 MediaSessionService的媒体通知样式
我使用Media3 1.0.0-rc01结合MediaSession Service构建了ExoPlayer,目前应用音频播放正常,系统自动生成的媒体通知也能正常显示,但我希望修改通知的文字颜色、背景色、图标及其他样式。
官方文档指出:要提供自定义通知,可通过DefaultMediaNotificationProvider.Builder创建MediaNotification.Provider,或自定义实现该Provider接口,再通过setMediaNotificationProvider将其添加到MediaSession中。
我已经创建了如下自定义Provider类:
package myapp; import android.content.Context; import android.os.Bundle; import androidx.media3.common.util.UnstableApi; import androidx.media3.session.CommandButton; import androidx.media3.session.DefaultMediaNotificationProvider; import androidx.media3.session.MediaNotification; import androidx.media3.session.MediaSession; import com.google.common.collect.ImmutableList; @UnstableApi public class MyMediaNotificationProvider implements MediaNotification.Provider { private Context context; public MyMediaNotificationProvider(Context context) { this.context = context; } @Override public MediaNotification createNotification( MediaSession mediaSession, ImmutableList<CommandButton> customLayout, MediaNotification.ActionFactory actionFactory, Callback onNotificationChangedCallback) { // Create the notification return new DefaultMediaNotificationProvider(context).createNotification( mediaSession, customLayout, actionFactory, onNotificationChangedCallback); } @Override public boolean handleCustomCommand( MediaSession session, String action, Bundle extras) { return false; } }
并在PlaybackService类中添加了setMediaNotificationProvider(new MyMediaNotificationProvider());来调用上述自定义类:
package myapp; import android.net.Uri; import androidx.media3.common.MediaItem; import androidx.media3.common.util.UnstableApi; import androidx.media3.exoplayer.ExoPlayer; import androidx.media3.session.MediaSession; import androidx.media3.session.MediaSessionService; @UnstableApi public class PlaybackService extends MediaSessionService { private MediaSession mediaSession = null; @Override public MediaSession onGetSession(MediaSession.ControllerInfo controllerInfo) { // If desired, validate the controller before returning the media session return mediaSession; } // Create your Player and MediaSession in the onCreate lifecycle event @Override public void onCreate() { super.onCreate(); // Create the player and the mediasession ExoPlayer player = new ExoPlayer.Builder(this).build(); mediaSession = new MediaSession.Builder(this, player).build(); // Build the media item. Uri mp3 = Uri.parse("android.resource://myapp/raw/mysoundtoplay"); MediaItem mediaItem = MediaItem.fromUri(mp3); // Set the media item to be played. player.setMediaItem(mediaItem); // loop mode player.setRepeatMode(player.REPEAT_MODE_ALL); // Prepare the player. player.prepare(); // Play player.setPlayWhenReady(true); // CALL MY NEW CLASS TO CREATE NOTIFICATION Context context = getApplicationContext(); // context must be passed to class setMediaNotificationProvider(new MyMediaNotificationProvider(context)); } // Remember to release the player and media session in onDestroy @Override public void onDestroy() { mediaSession.getPlayer().release(); mediaSession.release(); mediaSession = null; super.onDestroy(); } }
目前应用运行正常,但我还不清楚如何在现有代码基础上实现通知样式的定制(文字、颜色、图标等),希望能得到帮助。
内容的提问来源于stack exchange,提问作者Pepsi_34
相关产品推荐
相关产品推荐

