如何使用AVFoundation检测起飞的Airpod?

时间:2019-01-28 10:05:44

标签: ios avfoundation airpods

正如标题所述,我需要检测用户在播放音乐时摘下Airpods。由于某些原因,我必须使用AVAudioplayer而不是MPMusicPlayer播放音乐,并且只有当所有airpod起飞时AVAudioSession才能接收暂停事件。

是否有使用AVFoundation的方法可以检测到单个Airpod起飞?还是Apple不在AVFoundation中实现这一点?

1 个答案:

答案 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;
}];