MediaController 方法 adjustVolume 和 MediaSession

MediaController method adjustVolume and MediaSession

我正在实现一个使用 MediaController 和 MediaSession classes 的音频播放器。 我已经能够使用示例代码并实现 MediaSession.Callback class 来处理 MediaController.TransportControls 来实现传输控制(播放、暂停、倒带...)。另请注意,我对 Android 开发比较陌生。

我已经开始研究如何处理音频命令并注意到 MediaController 定义了 adjustVolume() 和 setVolumeTo() 方法。

我的问题是:为什么不以类似于传输命令的方式处理音量命令(以及可能的其他音频命令,如 BTBF、EQ 设置...)? 换句话说,为什么没有可以由 MediaSession 回调处理的 MediaController.AudioControls class?

我看到有一个 MediaController.PlaybackInfo class 提供了有关音频的各种信息。 MediaController class 中还有一个 sendCommand() 方法,由 MediaSession.Callback 中的 onCommand() 回调处理。类似地,MediaController.TransportControls 中定义了 SendCustomAction() 方法,它由 MediaSession.Callback 中的 onCustomAction() 处理。 我很想使用这些命令来调整音频播放的各个方面,但我想确保我没有忽略旨在实现此目的的 classes。似乎 MediaSession class 可以有额外的方法(额外的嵌套 class?)以类似于传输控制的方式处理音频控制。 提前致谢。 吉姆

MediaController 公开的 API 或控件是与从锁屏、通知或耳机媒体按钮进行远程控制有关的最常见用例。

  1. MediaController#sendCommand(String command, Bundle args, ResultReceiver cb) 专为您的应用程序 UI 和管理 MediaSession 的服务之间的应用程序特定自定义命令而设计。这非常适合您想到的特定用例 w.r.t EQ 设置。

  2. MediaController.TransportControls#sendCustomAction(...) on the other hand is for any PlaybackState.CustomAction(s) which can be displayed by other apps. An example of a CustomAction would be say marking an item as a favorite. (You can see how this is implemented in the MusicService class in the MediaBrowserService样本)

另请注意,可以在会话上设置 VolumeProvider class 以接收有关 changes/set 的回调。如果没有这个,会话将处理卷的更新。