【发布时间】:2015-10-26 16:09:58
【问题描述】:
我想知道我的AVAudioRecorder 何时无法访问(例如音乐开始播放时)。
由于 audioRecorderEndInterruption 将在 iOS 9 中被弃用,我将重点关注 AVAudioSession 的中断通知(但两者都没有按预期工作)。
问题是,如果应用程序在中断发生时一直并且仍然在前台,则永远不会调用中断通知。
例如:用户无需将应用程序移至后台即可开始和停止播放音乐。
要检测我正在使用的任何中断:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionWasInterrupted:) name:AVAudioSessionInterruptionNotification object:nil];
...
- (void)audioSessionWasInterrupted:(NSNotification *)notification {
if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
NSLog(@"Interruption notification");
if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
NSLog(@"InterruptionTypeBegan");
} else {
NSLog(@"InterruptionTypeEnded");
}
}
}
我按预期得到InterruptionTypeBegan,但如果应用程序仍在前台,则不会调用InterruptionTypeEnded(这意味着在应用程序置于后台并回到前台之前不会调用它)。
当应用在前台时发生中断时,我如何才能收到InterruptionTypeEnded 通知?
【问题讨论】:
-
此链接可能对您有所帮助:- stackoverflow.com/questions/23586056/…
-
来自苹果文档:“不能保证开始中断一定会有结束中断。您的应用需要注意切换到前台运行状态或用户按下播放按钮。在无论哪种情况,请确定您的应用是否应重新激活其音频会话。”
标签: ios avfoundation interrupt avaudiorecorder avaudiosession