由于au@R_301_410@RecorderEndInterruption将不再适用于iOS 9,因此我专注于AVAu@R_301_410@Session的中断通知(但是两者都无法正常工作).
问题是如果应用程序在发生中断时仍然处于前台,则不会调用中断通知.
检测我使用的任何中断:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(au@R_301_410@SessionWasInterrupted:) name:AVAu@R_301_410@SessionInterruptionNotification object:nil]; ... - (void)au@R_301_410@SessionWasInterrupted:(NSNotification *)notification { if ([notification.name isEqualToString:AVAu@R_301_410@SessionInterruptionNotification]) { NSLog(@"Interruption notification"); if ([[notification.userInfo valueForKey:AVAu@R_301_410@SessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAu@R_301_410@SessionInterruptionTypeBegan]]) { NSLog(@"InterruptionTypeBegan"); } else { NSLog(@"InterruptionTypeEnded"); } } }
我按预期得到InterruptionTypeBegan,但是如果应用程序仍然在前台,则不会调用InterruptionTypeEnded(这意味着在应用程序放置在后台并返回到前台之前不会被调用).
解决方法@H_502_18@
这是一个广泛的问题,影响任何使用AV框架组件的应用程序(与iOS应用程序相同).
正如在’s documentation关于音频中断的问题所解释的,InterruptionTypeEnded应该实际应用于所提到的场景:
If the user dismisses the interruption … the system invokes your callback method,indicating that the interruption has ended.
但是,它也指出,InterruptTypeEnded可能根本不被调用:
There is no guarantee that a begin interruption will have an end interruption.
因此,在上述情况下需要采用不同的方法.
当谈到处理音乐中断时,这个问题不会在很长一段时间. iOS 9有效地防止在调用应用程序的音频处理程序时使用外部音频源.
处理媒体中断的确切问题的方法可能是听MPMusicPlayerController的playbackState,如此stackoverflow问题:Detecting if music is playing?所示.
更直接的处理中断问题的方法是:
在InterruptionTypeBegan时通过重新调用音频组件来完全阻止音频中断.
或者通过给出外部媒体源已经中断了音频会话的UI指示(例如显示不活动的麦克风).
正如在’s documentation关于音频中断的问题所解释的,InterruptionTypeEnded应该实际应用于所提到的场景:
If the user dismisses the interruption … the system invokes your callback method,indicating that the interruption has ended.
但是,它也指出,InterruptTypeEnded可能根本不被调用:
There is no guarantee that a begin interruption will have an end interruption.
因此,在上述情况下需要采用不同的方法.
当谈到处理音乐中断时,这个问题不会在很长一段时间. iOS 9有效地防止在调用应用程序的音频处理程序时使用外部音频源.
处理媒体中断的确切问题的方法可能是听MPMusicPlayerController的playbackState,如此stackoverflow问题:Detecting if music is playing?所示.
更直接的处理中断问题的方法是:
在InterruptionTypeBegan时通过重新调用音频组件来完全阻止音频中断.
或者通过给出外部媒体源已经中断了音频会话的UI指示(例如显示不活动的麦克风).