2014-01-29 12 views
8

我的iOS 7應用程序在必要時發聲文本。如何讓我的應用程序音頻在講話時很好地中斷iPhone音頻

我希望做的是使用戶礦井運行時聽他的音樂或播客(或任何其他應用程序使用音頻)。

預期的行爲是別人音頻要麼mixe或鴨,當我說話的應用程序,那麼音頻別人在初始水平恢復他的音量之後。

我已經嘗試了許多方法來實現這一目標,但沒有什麼是足夠好,因爲我列出我所面臨的問題,代碼後。

我目前的實現是基於如下創建播放前或文本到語音會話:

+ (void)setAudioActive { 

    [[self class] setSessionActiveWithMixing:YES]; 
} 

播放/演講結束後,我把我對閒置如下:

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

其相應地處理會話建立到活動參數的核心功能,如下所示:

+ (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)當我將路由連接到藍牙時,底層音頻(如Podcast或iPod音樂)變得非常低,聽起來很吵,這使我的解決方案無法使用,我的用戶會拒絕這個質量等級的au。

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 ---"); 
    } 
} 
+0

你有沒有找到解決你的問題?我有你的問題編號2 – Jan

+0

唉,不。聽起來像沒有人知道如何做這些事情。你有沒有跑過任何奇怪的東西來幫助你? (提問以吸引注意力)@Jan –

+0

看起來這個帖子裏有很多問題。只是爲了清除問題,你是不是無法將應用的音頻與用戶的音樂混合?作爲您的解決方法的結果,外部和藍牙音頻展示*毛刺*? –

回答

-1

您的問題很可能是由於您設置的類別:AVAudioSessionCategoryPlayAndRecord。 PlayAndRecord類別不允許您的應用將音頻與其他應用混合。您應該再次參考音頻會話類別中的文檔:https://developer.apple.com/library/ios/documentation/avfoundation/reference/AVAudioSession_ClassReference/Reference/Reference.html。看起來AVAudioSessionCategoryAmbient可能更符合你的需求。

+1

PlayAndRecord類別允許鴨音頻選項。 – picciano

相關問題