我正在使用 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 再次运行吗?提前致谢!