我将 ExoPlayer 与库中的标准PlayerNotificationManager类一起使用。
当我关闭通过滑动播放音乐的应用程序时,Android 完全将其杀死,音乐停止,通知继续显示,但按钮未激活。我可以在不使用 Android Service 的情况下实现播放前台吗?如果是,该怎么做?也许 ExoPlayer 有允许实现它的标准服务?
播放器初始化:
SimpleExoPlayer exoPlayer = new SimpleExoPlayer.Builder(context, new DefaultRenderersFactory(context))
.setTrackSelector(new DefaultTrackSelector(context))
.build();
exoPlayer.setAudioAttributes(audioAttributes);
exoPlayer.setHandleAudioBecomingNoisy(true);
MediaSessionCompat mediaSession = new MediaSessionCompat(context, "Newtone");
MediaSessionConnector mediaSessionConnector = new MediaSessionConnector(mediaSession);
mediaSessionConnector.setPlayer(exoPlayer);
PlayerNotificationManager notificationManager = PlayerNotificationManager
.createWithNotificationChannel(
context, NOTIFICATION_CHANNEL_ID, R.string.app_name, R.string.app_name, NOTIFICATION_ID,
mediaDescriptionAdapter, notificationListener);
notificationManager.setPlayer(exoPlayer);
notificationManager.setMediaSessionToken(mediaSession.getSessionToken());
notificationManager.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
notificationManager.setUseNavigationActionsInCompactView(true);
notificationManager.setPriority(NotificationCompat.PRIORITY_MAX);
notificationManager.setUseChronometer(false);
notificationManager.setFastForwardIncrementMs(0);
notificationManager.setRewindIncrementMs(0);