我使用 Media3 1.0.0-rc01 和 mediasession 服务构建了一个 exoplayer,如 开发人员指南
中所述它自动生成媒体通知,我想更改颜色(文本、背景)和图标(等等)。
指南说:“要提供自定义通知,请使用 DefaultMediaNotificationProvider.Builder 创建 MediaNotification.Provider 或创建提供程序接口的自定义实现。使用 setMediaNotificationProvider 将提供程序添加到 MediaSession。”
所以我为提供者创建了一个新类:
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 {
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;
}
}
可能是我添加的“context”有问题,在debug中是“null”
我在 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
setMediaNotificationProvider(new MyMediaNotificationProvider());
}
// Remember to release the player and media session in onDestroy
@Override
public void onDestroy() {
mediaSession.getPlayer().release();
mediaSession.release();
mediaSession = null;
super.onDestroy();
}
}
应用程序运行良好(正在播放声音),但看不到任何通知 你能帮我吗?
那么如果我成功创建通知,也许我可以自定义它?谢谢
在我的播放服务中:
// CALL MY NEW CLASS TO CREATE NOTIFICATION
Context context = getApplicationContext(); // context must be passed to class
setMediaNotificationProvider(new MyMediaNotificationProvider(context));
在我的提供商中:
public class MyMediaNotificationProvider implements MediaNotification.Provider {
private Context context;
public MyMediaNotificationProvider(Context context) {
this.context = context;
}
现在我尝试使用 onUpdateNotification 自定义通知。
有人可以帮忙吗?