Reputation: 31
As the title says, I need to detect users taking off their airpods when playing music. For some reasons I must use AVAudioplayer rather than MPMusicPlayer to play music, and AVAudioSession only can receive pause event when all of airpods are taken off.
Is there any method with AVFoundation can detect single Airpod is taken off? Or Apple does not implement this in AVFoundation?
Upvotes: 2
Views: 861
Reputation: 31
Finally I addressed this issue by using MPRemoteCommandCenter
to receive pause event. The pause event could be triggered by one or more Airpods taken off. After receiving the event, I can pause the music played by AVAudioplayer in the handler block.
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[[MPRemoteCommandCenter sharedCommandCenter].pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
// Paused by one or more Airpods taken off !
return MPRemoteCommandHandlerStatusSuccess;
}];
Upvotes: 1