问题
In the article Handling External Player Events Notifications Apple says that you need to do this:
• 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.
The second part can be applied only on iOS and it works there. However, not sure how to handle this on macOS. I would assume that one thing needed to set is to set MPNowPlayingInfoCenter
. But it can't be checked in the Control Center on macOS, I would guess that the only method is by checking the TouchBar audio controls, but it doesn't seem to have effect there. So probably there is a need to set something like AVAudioSession
on iOS, but I can't find anything similar on macOS. And documentation is almost non-existent.
Note that I am using this in the text to speech app so I can't use some APIs like AVPlayer
.
回答1:
I found the answer - yes it is enough to set MPNowPlayingInfoCenter
, the critical part is to set playbackState
.
来源:https://stackoverflow.com/questions/54842139/how-to-detect-remote-audio-buttons-on-macos