陳俞安
陳俞安

Reputation: 31

How to Detect Single Airpod is Taken off using AVFoundation?

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

Answers (1)

陳俞安
陳俞安

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

Related Questions