我正在使用AUGraph和AUSampler將MIDI信號轉換爲音頻。該應用程序正常運行良好,但如果應用程序被打斷,例如通過電話或計時器出現問題。中斷後,AUGraph停止運行,沒有任何聲音。再次獲得聲音的唯一方法是重新啓動應用程序。我使用的是標準的方法來處理中斷:AUGraph在iOS中的電話中斷後停止運行
[[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再次運行?提前致謝!
如果你在handleInterruption中放置一個斷點:它會被打到嗎?值得注意的是,中斷通知系統是iOS6的新功能,因此可能無法在iOS5設備上運行。另外一種快速測試中斷的方法是激活Siri(按住home鍵)。 – 2013-04-06 09:42:04