2010-03-03 67 views
13

我試圖使用AVAudioPlayer,AVAudioSession和AudioSessionSetProperty將音頻輸出到藍牙耳機(不是A2DP)。如何將iPhone音頻路由到藍牙耳機

似乎有功能選擇藍牙耳機作爲輸入(kAudioSessionProperty_OverrideCategoryEnableBluetoothInput),但沒有等效的設置輸出。這是在Voicemail應用程序中完成的,您可以在其中選擇耳機,手機揚聲器或揚聲器電話。我已經嘗試了SessionCategories和AudioSession屬性的各種組合,但我似乎無法擊中一種可行的方法。

我確定有人已經想通了,分享一個例子嗎?

+0

似乎沒有人已經解決了這個問題(至少目前爲止沒有人談論過這個問題!)我已經在這個主題上打開了一張Apple支持憑單,並且會在我有這個問題時發佈回覆。 – Ethan

+0

你有什麼迴應? – Krishnan

+0

是的,我做過。換句話說,「不,你不能這樣做。」我不認爲工程師真的明白我的要求,因爲我能夠完成它。我會看看我是否可以將相關部分作爲答案發布。 – Ethan

回答

16

這個小測試爲我工作......它涉及設置藍牙耳機作爲輸入也(不知道這是你想要的)。抱歉關於代碼蹩腳的格式...

// create and set up the audio session 
AVAudioSession* audioSession = [AVAudioSession sharedInstance]; 
[audioSession setDelegate:self]; 
[audioSession setCategory: AVAudioSessionCategoryPlayAndRecord error: nil]; 
[audioSession setActive: YES error: nil]; 

// set up for bluetooth microphone input 
UInt32 allowBluetoothInput = 1; 
OSStatus stat = AudioSessionSetProperty (
         kAudioSessionProperty_OverrideCategoryEnableBluetoothInput, 
         sizeof (allowBluetoothInput), 
         &allowBluetoothInput 
         ); 
NSLog(@"status = %x", stat); // problem if this is not zero 

// check the audio route 
UInt32 size = sizeof(CFStringRef); 
CFStringRef route; 
OSStatus result = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &route); 
NSLog(@"route = %@", route);  
// if bluetooth headset connected, should be "HeadsetBT" 
// if not connected, will be "ReceiverAndMicrophone" 

// now, play a quick sound we put in the bundle (bomb.wav) 
CFBundleRef mainBundle = CFBundleGetMainBundle(); 
CFURLRef  soundFileURLRef; 
SystemSoundID soundFileObject; 
soundFileURLRef = CFBundleCopyResourceURL (mainBundle,CFSTR ("bomb"),CFSTR ("wav"),NULL); 
AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject); 
AudioServicesPlaySystemSound (soundFileObject);  // should play into headset 

希望幫助!

+0

這看起來像代碼我最終結束了,對不起,我沒有來這裏,並更快地標記;) – Ethan

+0

「文檔是錯誤的,它影響輸入和輸出。從Apple Coreaudio郵件列表(http://lists.apple.com/archives/coreaudio-api/2009/Oct/msg00030.html) – mtoy

+2

AudioSessionSetProperty自iOS7以來已被棄用。沒有使用AudioSessionSetProperty,這看起來如何? –

6

我能夠得到這個工作,但它花了一些工夫。我拼湊相關的代碼在這裏:

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&err]; 

UInt32 allowBluetoothInput = 1; 
AudioSessionSetProperty(
    kAudioSessionProperty_OverrideCategoryEnableBluetoothInput, 
    sizeof (allowBluetoothInput), 
    &allowBluetoothInput); 

也可以到源可用和藍牙,耳機,聽筒或揚聲器,但事情就變得非常投入在該點之間進行切換。我最終寫的音頻源管理器超過700行。

+5

我非常有興趣瞭解如何在可用的可用來源之間切換,您是否認爲可以公開該代碼或向您的個人資料添加一些信息,以便我可以與您聯繫? –

+0

http://stackoverflow.com/questions/20393249/use-audio-unit-i-o-to-create-audio-on-the-fly – madLokesh

+0

這一個工作。 – user523234