AVAudioSession中断通知仅触发一次

时间:2015-08-07 23:27:45

标签: ios nsnotificationcenter avaudiosession

在我的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 app时,该方法再次被触发(InterruptionEnded)。现在我重复与以前相同的步骤,方法根本不会被解雇,我需要重新启动我的应用程序以使其再次运行。有人可以说出可能出错的地方或指出一些示例实现。

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

1 个答案:

答案 0 :(得分:2)

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

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