崩溃:AVAudioSession在iOS中通知线程

时间:2018-01-29 12:52:27

标签: ios objective-c avplayer interrupt-handling audiotoolbox

我在AudioToolBox中发生EXC_BAD_ACCESS崩溃。如何正确处理中断?

请查看crashlytics屏幕截图以获取更多信息。 crashlytics screenshot

1 个答案:

答案 0 :(得分:3)

当我接到电话/ faceTime时,我的音频流播放器崩溃了。它实际上是一个非ARC的老班。只需为流类添加一个InterruptionNotification Observer,如果正在播放流类,我们需要在中断开始时暂停播放器实例。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruptionChangeToState:) name:@"ASAudioSessionInterruptionOccuredNotification" object:nil];

- (void)handleInterruptionChangeToState:(NSNotification *)notification {
AudioQueuePropertyID inInterruptionState = (AudioQueuePropertyID) [notification.object unsignedIntValue];
if (inInterruptionState == kAudioSessionBeginInterruption){
    if ([self isPlaying]) {
        [self pause];

        pausedByInterruption = YES; //a global Bool variable
    }
}
else if (inInterruptionState == kAudioSessionEndInterruption){
    AudioSessionSetActive( true );
    if ([self isPaused] && pausedByInterruption) {
        [self pause]; // this is actually resume
        pausedByInterruption = NO;
    }
}

}

希望它有所帮助。