正如标题所述,我需要检测用户在播放音乐时摘下Airpods。由于某些原因,我必须使用AVAudioplayer而不是MPMusicPlayer播放音乐,并且只有当所有airpod起飞时AVAudioSession才能接收暂停事件。
是否有使用AVFoundation的方法可以检测到单个Airpod起飞?还是Apple不在AVFoundation中实现这一点?
答案 0 :(得分:0)
最后,我通过使用MPRemoteCommandCenter
来接收暂停事件解决了此问题。暂停事件可能由一个或多个Airpods起飞触发。收到事件后,我可以在处理程序块中暂停AVAudioplayer播放的音乐。
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[[MPRemoteCommandCenter sharedCommandCenter].pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
// Paused by one or more Airpods taken off !
return MPRemoteCommandHandlerStatusSuccess;
}];