我正在使用AUGraph和AUSampler将MIDI信号转换为音频。该应用程序可以正常运行,但是如果该应用程序被电话或计时器打断,则会出现问题。中断后,AUGraph停止运行,没有任何声音。再次获取声音的唯一方法是重新启动应用程序。我正在使用标准方法来处理中断:

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(handleInterruption:)
                                             name: AVAudioSessionInterruptionNotification
                                           object: [AVAudioSession sharedInstance]];

发生中断时,将调用handleInterruption方法:
// If the system interrupts the audio session - kill the volume
-(void) handleInterruption: (NSNotification *) notification {
    NSDictionary *interuptionDict = notification.userInfo;
    NSInteger interuptionType = [[interuptionDict valueForKey:AVAudioSessionInterruptionTypeKey] intValue];

    if (interuptionType == AVAudioSessionInterruptionTypeBegan) {
        [self setAudioSessionActive: NO];
        [self stopAUGraph];
    }
    else if (interuptionType == AVAudioSessionInterruptionTypeEnded) {
        [self setAudioSessionActive: YES];
        [self startAUGraph];
    }
}

这是启动和停止AUGraph的方法
-(void) startAUGraph {
    CheckError(AUGraphStart(_audioGraph), "Failed to start Audio Graph");
}

-(void) stopAUGraph {
    Boolean isRunning = false;

    // Check to see if the graph is running.
    CheckError(AUGraphIsRunning(_audioGraph, &isRunning), "Error trying querying whether graph is running");
    // If the graph is running, stop it.
    if (isRunning) {
        CheckError(AUGraphStop(_audioGraph), "Failed to stop Audio Graph");
    }
}

这是setAudioSessionActive方法:
-(void) setAudioSessionActive: (BOOL) active {
    NSError *audioSessionError = nil;
    BOOL success = [_audioSession setActive:active error:&audioSessionError];
    if (!success) {
        NSLog (@"Error activating audio session!");
    }
}

我在渲染回调中设置了一个断点,以便可以确认AUGraph没有运行。有人遇到过这个问题吗?我还需要做其他事情才能使AUGraph重新运行吗?提前致谢!

最佳答案

这个错误也给我带来了麻烦。我想我找到了一种解决方法:

由于NSNotifications在通话中断后返回时不会触发,因此在我的AppDelegate的-applicationWillEnterForeground:方法中,我检查是否存在导致其进入后台的中断(我将AVAudioSessionInterruptionNotificationAVAudioSessionInterruptionTypeBegan一起发布时设置了一个标志中断类型键),如果是的话,我在控制运行以下内容的AUGraph的对象中调用一个方法:

    Boolean isRun = FALSE;
    AUGraphIsRunning(myAUGraph, &isRun);
    if (isRun) {
        NSLog(@"END INTERRUPTION BUT AUGRAPH IS RUNNING");
        AUGraphStop(myAUGraph);// Kick start the AUGraph!
        AUGraphStart(myAUGraph); //Restart immediately
        Boolean isInit = FALSE;
        AUGraphIsInitialized(processingGraph, &isInit);
        if (!isInit) {
            NSLog(@"augraph not initd'");
            OSStatus result = AUGraphInitialize(processingGraph);
            if (result != noErr) {
                NSLog(@">>can't init graph");
            }
        }
    }
    else
    {
        NSLog(@"END INTERRUPTION BUT AUGRAPH IS NOT RUNNING");
    }

关键似乎正在停止,然后立即重新启动AUGraph。到目前为止,当有电话打入或另一个应用接管扬声器/麦克风时,它可以正常运行,我可以在我的应用中停下来的地方接听电话。

AUGraph的重新初始化似乎永远不会执行,除了isRun之外,TRUE也不会执行任何操作,但是我还是将其保留了下来。希望能对您的情况有所帮助,我花了几天的时间来破解此问题,我希望它能继续发挥作用,而不仅仅是巧合!到目前为止,还需要4个小时!

08-16 14:11