AVAudioSession 中断通知只触发一次

AVAudioSession Interruption notification fires only once

在我的 iOS 应用程序中,我正在像这样注册 AVAudioSession 中断通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAudioSessionInterruption:) name:AVAudioSessionInterruptionNotification object:nil];

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];

[[AVAudioSession sharedInstance] setActive:YES error:nil];

当我通过播放一些来自 youtube 的随机音乐来测试我的应用程序时,handleAudioSessionInterruption 方法首次触发 (InterruptionBegan)。当我退出 youtube 应用程序时,该方法再次被触发 (InterruptionEnded)。现在我重复之前的相同步骤,方法根本没有被触发,我需要重新启动我的应用程序以使其再次运行。谁能告诉我哪里出了问题或指出一些示例实现。

注意:我既没有看到任何错误,也没有看到应用程序崩溃。

观察者可能会从 NSNotificationCenter 中移除。确保没有发生这种情况。在不指定通知名称的情况下调用 removeObserver:self 会将自己从 NSNotificationCenter 中删除所有通知。

除了您当前正在注册的观察者之外,还尝试让 AppDelegate 成为 AVAudioSessionInterruptionNotification 的观察者。如果 AppDelegate 的处理程序即使在您的其他处理程序停止被调用后仍继续被调用,那么这将为您提供更多信息。