首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS中的电话呼叫中断后,AUGraph停止运行

iOS中的电话呼叫中断后,AUGraph停止运行
EN

Stack Overflow用户
提问于 2013-04-06 07:09:54
回答 2查看 2K关注 0票数 5

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

代码语言:javascript
复制
[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(handleInterruption:)
                                             name: AVAudioSessionInterruptionNotification
                                           object: [AVAudioSession sharedInstance]];

当出现中断时,将调用handleInterruption方法:

代码语言:javascript
复制
// 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的方法

代码语言:javascript
复制
-(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方法:

代码语言:javascript
复制
-(void) setAudioSessionActive: (BOOL) active {
    NSError *audioSessionError = nil;
    BOOL success = [_audioSession setActive:active error:&audioSessionError];
    if (!success) {
        NSLog (@"Error activating audio session!");
    }
}

我在渲染回调中设置了一个断点,这样我就可以确认AUGraph没有运行。以前有没有人遇到过这个问题?要让AUGraph重新运行,我还需要做些什么吗?提前感谢!

EN

回答 2

Stack Overflow用户

发布于 2013-10-21 02:24:50

这个bug也给我带来了很大的麻烦。我想我找到了一个解决方法:

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

代码语言:javascript
复制
    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个小时,到目前为止还在计算!

票数 4
EN

Stack Overflow用户

发布于 2013-11-04 18:24:38

在iOS 7中,它适用于我。

代码语言:javascript
复制
//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];

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15845064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档