我使用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重新运行,我还需要做些什么吗?提前感谢!
发布于 2013-10-21 02:24:50
这个bug也给我带来了很大的麻烦。我想我找到了一个解决方法:
由于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个小时,到目前为止还在计算!
发布于 2013-11-04 18:24:38
在iOS 7中,它适用于我。
//Interruption handler
-(void) interruption: (NSNotification*) aNotification
{
NSDictionary *interuptionDict = aNotification.userInfo;
NSNumber* interuptionType = (NSNumber*)[interuptionDict valueForKey:AVAudioSessionInterruptionTypeKey];
if([interuptionType intValue] == AVAudioSessionInterruptionTypeBegan)
[self stopAUGraph];
else if ([interuptionType intValue] == AVAudioSessionInterruptionTypeEnded)
[self startAUGraph];
}https://stackoverflow.com/questions/15845064
复制相似问题