如何检测 macOS 上的远程音频按钮?

How to detect remote audio buttons on macOS?

文章中Handling External Player Events Notifications 苹果说你需要这样做:

• Use the shared MPRemoteCommandCenter object to register handlers for the events you wish to handle and to disable the events you are not interested in receiving.

• Begin playing audio. Your app must be the Now Playing app. An app does not receive remote control events until it begins playing audio. Test that your app is properly receiving and handling remote control events with Control Center, which you access by swiping up from the bottom edge of your screen. These controls send remote control events to the app that is currently or was most recently playing audio. You can also access the playback controls from the lock screen of the device.

第二部分只能在 iOS 上应用,并且可以在那里工作。但是,不确定如何在 macOS 上处理此问题。我假设需要设置的一件事是设置 MPNowPlayingInfoCenter。但是在macOS的控制中心是查不到的,估计只能通过TouchBar的音频控件来查了,不过好像没有效果。所以可能需要在 iOS 上设置类似 AVAudioSession 的东西,但我在 macOS 上找不到类似的东西。而且文档几乎不存在。

请注意,我在文本到语音应用程序中使用它,所以我不能使用某些 API,例如 AVPlayer

我找到了答案 - 是的,设置 MPNowPlayingInfoCenter 就足够了,关键部分是设置 playbackState.