相关疑难解决方法(0)

iOS AVAudioSession中断通知无法按预期工作

我想知道我何时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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

InterruptionTypeBegan按预期得到了,但InterruptionTypeEnded如果应用程序仍在前台,则不会被调用(意味着在将应用程序放置在后台并返回到前台之前不会调用它).

InterruptionTypeEnded当应用程序在前台时发生中断时,我如何收到通知?

interrupt avfoundation avaudiorecorder ios avaudiosession

18
推荐指数
1
解决办法
1万
查看次数