【发布时间】:2022-01-24 07:47:03
【问题描述】:
我正在使用 exo 播放器通知管理器来显示我的歌曲媒体播放通知,但不幸的是 seekbar 拇指、当前位置和最大持续时间没有显示,我现在得到的只是那个进度条。
这是我用来启动通知的前台服务
public class audioPlayerService extends Service {
private SimpleExoPlayer player;
private PlayerNotificationManager playerNotificationManager;
private MediaSessionCompat mediaSession;
private CacheDataSource.Factory factory;
private DefaultDataSourceFactory dataSourceFactory;
private Bitmap icon;
@Override
public void onCreate() {
super.onCreate();
final Context context = this;
player = new SimpleExoPlayer.Builder(context).build();
dataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, context.getResources().getString(R.string.app_name)));
factory = new CacheDataSource.Factory().setCache(downloadUtil.getCache(context))
.setUpstreamDataSourceFactory(dataSourceFactory);
playerNotificationManager = new PlayerNotificationManager.Builder(context, 151,
context.getResources().getString(R.string.app_name))
.setChannelNameResourceId(R.string.app_name)
.setChannelImportance(IMPORTANCE_HIGH)
.setMediaDescriptionAdapter(new PlayerNotificationManager.MediaDescriptionAdapter() {
@Override
public CharSequence getCurrentContentTitle(Player player) {
return player.getCurrentMediaItem().mediaMetadata.displayTitle;
}
@Nullable
@Override
public PendingIntent createCurrentContentIntent(Player player) {
return null;
}
@Nullable
@Override
public CharSequence getCurrentContentText(Player player) {
return player.getCurrentMediaItem().mediaMetadata.artist;
}
@Nullable
@Override
public Bitmap getCurrentLargeIcon(Player player, PlayerNotificationManager.BitmapCallback callback) {
if (icon == null) {
icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.logo);
}
Glide.with(context)
.asBitmap()
.load(player.getCurrentMediaItem().mediaMetadata.artworkUri)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
icon=resource;
callback.onBitmap(resource);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
return icon;
}
}).setNotificationListener(new PlayerNotificationManager.NotificationListener() {
@Override
public void onNotificationCancelled(int notificationId, boolean dismissedByUser) {
stopSelf();
}
@Override
public void onNotificationPosted(int notificationId, Notification notification, boolean ongoing) {
if (ongoing) {
// Here Audio is playing, so we need to make sure the service will not get destroyed by calling startForeground.
startForeground(notificationId, notification);
} else {
//Here audio has stopped playing, so we can make notification dismissible on swipe.
stopForeground(false);
}
}
})
.build();
playerNotificationManager.setPlayer(player);
playerNotificationManager.setSmallIcon(R.drawable.logo);
playerNotificationManager.setUseNextAction(false);
playerNotificationManager.setUsePreviousAction(false);
playerNotificationManager.setUseNextActionInCompactView(false);
playerNotificationManager.setUsePreviousActionInCompactView(false);
playerNotificationManager.setUseChronometer(true);
mediaSession = new MediaSessionCompat(context, context.getResources().getString(R.string.app_name));
mediaSession.setActive(true);
MediaSessionConnector mediaSessionConnector =
new MediaSessionConnector(mediaSession);
mediaSessionConnector.setPlayer(player);
playerNotificationManager.setMediaSessionToken(mediaSession.getSessionToken());
}
@Override
public void onDestroy() {
playerNotificationManager.setPlayer(player);
player.release();
player = null;
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
BookElement book = intent.getParcelableExtra("track");
mediaSession.setMetadata(new MediaMetadataCompat.Builder().putLong(MediaMetadataCompat.METADATA_KEY_DURATION,
(long) (book.getSoundFile().getDuration() * 1000)).build());
MediaItem mediaItem = new MediaItem.Builder()
.setUri(Uri.parse(book.getSoundFile().getLocation()))
.setMediaMetadata(new MediaMetadata.Builder()
.setArtworkUri(Uri.parse(book.getThumbnail().getLocation()))
.setArtist(book.getAuthor().getName())
.setMediaUri(Uri.parse(book.getSoundFile().getLocation()))
.setDisplayTitle(book.getTitle())
.build())
.build();
MediaSource source = new ProgressiveMediaSource.Factory(factory).createMediaSource(mediaItem);
player.prepare(source);
player.setPlayWhenReady(true);
return START_STICKY;
}}
还有我的缓存类
public class downloadUtil {
private static Cache cache;
private static DownloadManager downloadManager;
public static synchronized Cache getCache(Context context){
if (cache==null){
File cacheDirectory=new File(context.getExternalFilesDir(null),"downloads");
cache=new SimpleCache(cacheDirectory,new NoOpCacheEvictor());
}
return cache;
}
public static synchronized DownloadManager getDownloadManager(Context context){
if (downloadManager==null){
Executor downloadExecutor = Runnable::run;
downloadManager =
new DownloadManager(context,new StandaloneDatabaseProvider(context),getCache(context),
new DefaultHttpDataSource.Factory(),downloadExecutor);
downloadManager.setMaxParallelDownloads(3);
}
return downloadManager;
}}
【问题讨论】:
标签: java android kotlin exoplayer