android - 在前台播放 ExoPlayer
问题描述
我将 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);
解决方案
推荐阅读
- node.js - 生成 reactjs 构建时缺少 Index.html。我们如何将 react 和 nodejs 项目部署到生产环境中?
- angular - 在 Angular 中自定义 FullCalender.io 视图和样式
- jquery - 如何从动态加载的选项中禁用选定的选项
- c# - Dockerfile 使用适用于 Windows 10 和 sdk 3.0 的 Powershell 构建错误
- php - Laravel 雄辩的 with() 返回 null
- python - 如何在 tkinter 中创建一个条目 int
- google-chrome - 打开单个 Chrome 选项卡会导致多个远程连接
- bash - UNIX:条件检查导致意外结果
- json - 使用 CLI 从 Windows 系统中的 AWS S3 存储桶将 JSON 文件复制到本地
- python - 适用于 Apple 环境的 Python 脚本