如何让我的应用程序audio很好地中断说话时的iPhoneaudio

我的iOS 7应用程序在必要时发声文本。

我想要做的是让用户在我的运行过程中收听他的音乐或播客(或任何其他使用audio的应用程序)。

预期的行为是,当我的应用程序说话时,其他audio混音或鸭子,然后其他audio恢复在初始阶段的音量。

我已经尝试了很多方法来实现这个目标,但是没有什么是不够的,因为我在代码之后列出了我面对的问题。

我目前的实施是基于在播放或文本到语音之前创build会话如下:

+ (void)setAudioActive { [[self class] setSessionActiveWithMixing:YES]; } 

演讲/演讲结束后,我把我设置为闲置状态如下:

 + (void)setAudioIdle { [[self class] setSessionActiveWithMixing:NO]; } 

处理会话设置的核心function对应于激活的参数,如下所示:

 + (void)setSessionActiveWithMixing:(BOOL)active { NSError *error = NULL; BOOL success; AVAudioSession *session = [AVAudioSession sharedInstance]; static NSInteger counter = 0; success = [session setActive:NO error:&error]; if (error) { DDLogError(@"startAudioMixAndBackground: session setActive:NO, %@", error.description); } else { counter--; if (counter<0) counter = 0; } if (active) { AVAudioSessionCategoryOptions options = AVAudioSessionCategoryOptionAllowBluetooth //|AVAudioSessionCategoryOptionDefaultToSpeaker |AVAudioSessionCategoryOptionDuckOthers ; success = [session setCategory://AVAudioSessionCategoryPlayback AVAudioSessionCategoryPlayAndRecord withOptions: options error: &error]; if (error) { // Do some error handling DDLogError(@"startAudioMixAndBackground: setCategory:AVAudioSessionCategoryPlayback, %@", error.description); } else { //activate the audio session success = [session setActive:YES error:&error]; if (error) { DDLogError(@"startAudioMixAndBackground: session setActive:YES, %@", error.description); } else { counter++; } } } DDLogInfo(@"Audio session counter is: %ld",counter); } 

我目前的问题是:

1)当我的应用程序开始说话时,我听到一些声音中出现小故障的国王,这使得它不好。

2)当我连接到蓝牙路由时,底层audio(如Podcast或iPod音乐)变得非常低,听起来很嘈杂,这使我的解决scheme只是无法使用,我的用户会拒绝这个质量等级。

3)当其他蓝牙连接的设备试图发出声音(例如在汽车或实例中的GPS),我的应用程序不会收到任何中断(或我错误地处理),请参阅我的代码如下:

 - (void)startAudioMixAndBackground { // initialize our AudioSession - // this function has to be called once before calling any other AudioSession functions [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionDidChangeInterruptionType:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]]; // set our default audio session state [[self class] setAudioIdle]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; if ([self canBecomeFirstResponder]) { [self becomeFirstResponder]; } @synchronized(self) { self.okToPlaySound = YES; } //MPVolumeSettingsAlertShow(); } // want remote control events (via Control Center, headphones, bluetooth, AirPlay, etc.) - (void)remoteControlReceivedWithEvent:(UIEvent *)event { if (event.type == UIEventTypeRemoteControl) { switch(event.subtype) { case UIEventSubtypeRemoteControlPause: case UIEventSubtypeRemoteControlStop: [[self class] setAudioIdle]; break; case UIEventSubtypeRemoteControlPlay: [[self class] setAudioActive]; break; default: break; } } } #pragma mark - Audio Support - (void)audioSessionDidChangeInterruptionType:(NSNotification *)notification { AVAudioSessionInterruptionType interruptionType = [[[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey] unsignedIntegerValue]; if (AVAudioSessionInterruptionTypeBegan == interruptionType) { DDLogVerbose(@"Session interrupted: --- Begin Interruption ---"); } else if (AVAudioSessionInterruptionTypeEnded == interruptionType) { DDLogVerbose(@"Session interrupted: --- End Interruption ---"); } } 

您的问题很可能是由于您设置的类别:AVAudioSessionCategoryPlayAndRecord。 PlayAndRecord类别不允许您的应用将audio与其他应用混合。 您应该再次参考audio会话类别上的文档: https : //developer.apple.com/library/ios/documentation/avfoundation/reference/AVAudioSession_ClassReference/Reference/Reference.html 。 看起来AVAudioSessionCategoryAmbient可能更符合你的要求。