7

好吧,我有我的AVAudioSession定義與以下(是,混合的C和OBJ - C調用)另外請注意,該應用程序具有背景模式音頻,因爲如果記錄它必須繼續這樣做,而應用程序是在後臺:AVAudioSession /音頻會話服務切換輸出

[(AVAudioSession *)[AVAudioSession sharedInstance] setDelegate: self]; 
// Allow the app sound to continue to play when the screen is locked. 
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 
//Turn off automatic gain on the microphone 
[[AVAudioSession sharedInstance] setMode:AVAudioSessionModeMeasurement error:nil]; 
//Turn on the ability to mix with others 
UInt32 doSetProperty = 1; 
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty); 

//Activate the audio session 
[[AVAudioSession sharedInstance] setActive:YES error:nil]; 

應用程序有音頻的幾個不同的選項(只有前兩個已經被編程):

  • 標準:沒有從應用的音頻,所以不要與外部音頻混淆
  • 只記錄耳機麥克風的:必須有沒有增益調整, 不惹外界音頻應用程序(將通過耳機打)從
  • 播放音樂,沒有記錄:停止對外音頻,並通過當前播放輸出(揚聲器或耳機)
  • 從應用程序和記錄耳機麥克風播放音樂:停止對外音頻,記錄並只能通過耳機中播放

記錄在前臺和後臺運作良好,我會添加稍後再玩。不過,我今晚才注意到,如果音頻已經在播放(Pandora)的揚聲器上,並且進入我的應用程序並激活錄音模式,則它會切換潘多拉盒子以通過電話揚聲器播放,並且即使在聽音停止之後,應用程序背景(但不強制關閉),音頻繼續通過手機揚聲器播放,直到我強制關閉應用程序。

//Deactivate the audio session 
[[AVAudioSession sharedInstance] setActive:NO error:nil]; 

在另一面,如果耳機是在和正在播放的音樂通過耳機時,應用程序啓動進入拍攝模式,則只有一個短暫的停頓和音樂將繼續在同一打音量(正確,沒有迴避)。

我看不出有任何理由爲什麼當我在沒有耳機激活音頻會話的路線應該改變,爲什麼當會話被禁用它不會更改回。尤其是換到電話音箱!有什麼我做錯了,或者我只需要根據用戶想要做什麼而不同地定義AVAudioSession(而不是一攬子播放+記錄和模式測量)?即使我需要爲不同的用例分別定義它。例如,如果應用正在錄製,則音頻將始終通過耳機,並且如果該應用不錄製,則音頻輸出耳機或揚聲器(取決於用戶是否插入了耳機) - 即正常行爲)。

其他細節

好吧,我嘗試切換所有代碼C,發現埋在文檔內約kAudioSessionProperty_OverrideAudioRoute

kAudioSessionOverrideAudioRoute_None
指定一個音符,爲kAudioSessionCategory_PlayAndRecord的類別,即輸出音頻應該去接收器。這是此類別的默認輸出音頻路由。

於是我嘗試了3周不同的方式設置該屬性(這些屬性):

  • kAudioSessionProperty_OverrideAudioRoute與kAudioSessionOverrideAudioRoute_Speaker繼續播放音頻,但它切換出通過接收器,並顯示爲ReceiverAndMicrophone路線
  • kAudioSessionProperty_OverrideCategoryDe​​faultToSpeaker與kAudioSessionOverrideAudioRoute_Speaker停止當前播放的音頻,並顯示爲SpeakerAndMicrophone
  • kAud路線ioSessionProperty_OverrideCategoryDe​​faultToSpeaker爲1的值,做同樣的事情作爲kAudioSessionOverrideAudioRoute_Speaker

所以基本上文檔說默認是切換到接收器。但是,無論我做什麼,我都無法保持揚聲器輸出並保持外部音頻播放。

+0

嗨...你有任何解決方案?我在這裏面臨同樣的問題。 – 2015-10-13 07:04:36

回答

8

顯然,設置屬性的順序很重要,雖然在文檔中沒有提到這一點。對於那些最終發現這一點,這是什麼工作:

//Init and set the interrupt listener. last parameter is passed to interruptlistener 
AudioSessionInitialize(NULL, NULL, interruptlistener, NULL); 

//Allow the app sound to continue to play when the screen is locked. 
UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord; 
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory); 

//Force current audio out through speaker 
UInt32 routeSpeaker = kAudioSessionOverrideAudioRoute_Speaker; 
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(routeSpeaker), &routeSpeaker); 

//Turn on the ability to mix with others 
UInt32 doSetProperty = 1; 
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);