需要注意的是,只有iOS10以上才支持app被唤醒后在后台/锁屏状态下播放音频。所以iOS10以下的设备,在收到VoIP Push后只能在local push上设定一段固定铃声,这也是为什么iOS10以下只有“微信支付收款到账”,而没有后面具体的金额数值。
三、静音开关检测
不幸的是,在产品发布后没多久就受到了某互联网大佬的吐槽。

从产品体验上来说,收款到账的金额播报是随着local push的弹出一起播放的,更像是一种特殊的push铃声,而苹果对push铃声的处理是受到静音开关控制的,所以讲道理,这个吐槽是合理的。然而前面提到App在被VoIP Push唤醒之后,需要将AudioSessionCategory设置为AVAudioSessionCategoryPlayback或AVAudioSessionCategoryPlayAndRecord才可以在后台播放音频文件,这两种模式是不受静音开关控制的。要实现这个需求,就必须获取当前静音开关的状态。而苹果在iOS5之后并没有明确地提供一种方式让开发获取静音开关的状态,这就陷入了一个尴尬的局面。
苹果在iOS5之前可以使用以下方式监听静音键开关
- (BOOL)isMuted
{
CFStringRef route;
UInt32 routeSize = sizeof(CFStringRef);
OSStatus status = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &routeSize, &route);
if (status == kAudioSessionNoError)
{
if (route == NULL || !CFStringGetLength(route))
return YES;
}
return NO;
}
苹果在iOS5之后便禁止了使用这种方式监听静音按键,背后的原因应该是苹果希望开发者使用AVAudioSession来提供统一的音频播放效果。
最后我在Reddit上找到了一种曲线救国的方式,实现起来也不复杂:使用AudioServicesPlaySystemSound播放一段0.2s的空白音频,并监听音频播放完成事件,如果从开始播放到回调完成方法的间隔时间小于0.1s,则意味当前静音开关为开启状态。
void SoundMuteNotificationCompletionProc(SystemSoundID ssID,void* clientData){
MMSoundSwitchDetector* detecotr = (__bridge MMSoundSwitchDetector*)clientData;
[detecotr complete];
}
- (instancetype)init {
self = [super init];
if (self) {
NSURL *pathURL = [[NSBundle mainBundle] URLForResource:@"mute" withExtension:@"caf"];
if (AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &_soundId) == kAudioServicesNoError){
AudioServicesAddSystemSoundCompletion(self.soundId, CFRunLoopGetMain(), kCFRunLoopDefaultMode, SoundMuteNotificationCompletionProc,(__bridge void *)(self));
UInt32 yes = 1;
AudioServicesSetProperty(kAudioServicesPropertyIsUISound, sizeof(_soundId),&_soundId,sizeof(yes), &yes);
} else {
MMErrorWithModule(LOGMODULE, @"Create Sound Error.");
_soundId = 0;
}
}
return self;
}
- (void)checkSoundSwitchStatus:(CheckSwitchStatusCompleteBlk)completHandler {
if (self.soundId == 0) {
completHandler(YES);
return;
}
self.completeHandler = completHandler;
self.beginTime = CACurrentMediaTime();
AudioServicesPlaySystemSound(self.soundId);
}
- (void)complete {
CFTimeInterval elapsed = CACurrentMediaTime() - self.beginTime;
BOOL isSwitchOn = elapsed > 0.1;
if (self.completeHandler) {
self.completeHandler(isSwitchOn);
}
}










