ios – 如何让我的应用音频在讲话时很好地中断iPhone音频

前端之家收集整理的这篇文章主要介绍了ios – 如何让我的应用音频在讲话时很好地中断iPhone音频前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的iOS 7应用程序在必要时发出文本.

我想做的是让用户在我的运行时收听他的音乐或播客(或使用音频的任何其他应用).

预期的行为是,当我的应用说话时,其他音频会混音或躲避,然后其他音频会在之后的初始级别恢复音量.

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

我目前的实现基于在播放或文本到语音之前创建会话,如下所示:

  1. + (void)setAudioActive {
  2.  
  3. [[self class] setSessionActiveWithMixing:YES];
  4. }

播放/演讲结束后,我将我设置为闲置如下:

  1. + (void)setAudioIdle {
  2. [[self class] setSessionActiveWithMixing:NO];
  3. }

核心功能,根据活动参数处理会话设置,如下所示:

  1. + (void)setSessionActiveWithMixing:(BOOL)active
  2. {
  3. NSError *error = NULL;
  4. BOOL success;
  5.  
  6. AVAudioSession *session = [AVAudioSession sharedInstance];
  7.  
  8. static NSInteger counter = 0;
  9.  
  10. success = [session setActive:NO error:&error];
  11. if (error) {
  12. DDLogError(@"startAudioMixAndBackground: session setActive:NO,%@",error.description);
  13. }
  14. else {
  15. counter--; if (counter<0) counter = 0;
  16. }
  17.  
  18. if (active) {
  19. AVAudioSessionCategoryOptions options = AVAudioSessionCategoryOptionAllowBluetooth
  20. //|AVAudioSessionCategoryOptionDefaultToSpeaker
  21. |AVAudioSessionCategoryOptionDuckOthers
  22. ;
  23.  
  24.  
  25. success = [session setCategory://AVAudioSessionCategoryPlayback
  26. AVAudioSessionCategoryPlayAndRecord
  27. withOptions: options
  28. error: &error];
  29. if (error) {
  30. // Do some error handling
  31. DDLogError(@"startAudioMixAndBackground: setCategory:AVAudioSessionCategoryPlayback,error.description);
  32. }
  33. else {
  34. //activate the audio session
  35. success = [session setActive:YES error:&error];
  36. if (error) {
  37. DDLogError(@"startAudioMixAndBackground: session setActive:YES,error.description);
  38. }
  39. else {
  40. counter++;
  41. }
  42. }
  43. }
  44.  
  45. DDLogInfo(@"Audio session counter is: %ld",counter);
  46. }

我目前的问题是:

1)当我的应用程序开始讲话时,我听到声音中有一些小故障,这使它不好;

2)当我将路由连接到蓝牙时,基础音频(比如播客或iPod音乐)变得非常低并且听起来很嘈杂,这使得我的解决方案仅仅无法使用,我的用户将拒绝此级别的低质量.

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

  1. - (void)startAudioMixAndBackground {
  2.  
  3. // initialize our AudioSession -
  4. // this function has to be called once before calling any other AudioSession functions
  5. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionDidChangeInterruptionType:)
  6. name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];
  7.  
  8. // set our default audio session state
  9. [[self class] setAudioIdle];
  10.  
  11. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
  12.  
  13. if ([self canBecomeFirstResponder]) {
  14. [self becomeFirstResponder];
  15. }
  16.  
  17. @synchronized(self) {
  18. self.okToPlaySound = YES;
  19. }
  20.  
  21. //MPVolumeSettingsAlertShow();
  22. }
  23. // want remote control events (via Control Center,headphones,bluetooth,AirPlay,etc.)
  24. - (void)remoteControlReceivedWithEvent:(UIEvent *)event
  25. {
  26. if (event.type == UIEventTypeRemoteControl)
  27. {
  28. switch(event.subtype)
  29. {
  30. case UIEventSubtypeRemoteControlPause:
  31. case UIEventSubtypeRemoteControlStop:
  32. [[self class] setAudioIdle];
  33. break;
  34. case UIEventSubtypeRemoteControlPlay:
  35. [[self class] setAudioActive];
  36. break;
  37. default:
  38. break;
  39. }
  40. }
  41. }
  42.  
  43. #pragma mark - Audio Support
  44.  
  45. - (void)audioSessionDidChangeInterruptionType:(NSNotification *)notification
  46. {
  47. AVAudioSessionInterruptionType interruptionType = [[[notification userInfo]
  48. objectForKey:AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
  49.  
  50. if (AVAudioSessionInterruptionTypeBegan == interruptionType)
  51. {
  52. DDLogVerbose(@"Session interrupted: --- Begin Interruption ---");
  53. }
  54. else if (AVAudioSessionInterruptionTypeEnded == interruptionType)
  55. {
  56. DDLogVerbose(@"Session interrupted: --- End Interruption ---");
  57. }
  58. }

解决方法

您的问题很可能是由于您正在设置的类别:AVAudioSessionCategoryPlayAndRecord. PlayAndRecord类别不允许您的应用与其他应用混合/播放音频.你应该在这里再次引用音频会话类别的文档: https://developer.apple.com/library/ios/documentation/avfoundation/reference/AVAudioSession_ClassReference/Reference/Reference.html.看起来AVAudioSessionCategoryAmbient可能更像你想要的.

猜你在找的iOS相关文章