在關心耳機拔出等事件的類中添加如下方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(outputDeviceChanged:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];
- (void)outputDeviceChanged:(NSNotification *)aNotification
{
// do your jobs here
}
請注意,addobserver的參數填寫:其中的object必須是[AVAudioSession sharedInstance],而不是我們通常很多情況下填寫的nil,此處若為nil,通知也不會觸發。
為了改這個bug我打了三次包。