我正在使用 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:
方法中我检查是否有中断导致它进入后台(我当 AVAudioSessionInterruptionNotification
以 AVAudioSessionInterruptionTypeBegan
作为中断类型键发布时设置一个标志),如果是这样,我在控制运行以下 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 小时,还在继续!
关于iphone - iOS 电话中断后 AUGraph 停止运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15845064/