2016-01-27 128 views
0

我想使用設備上的內置麥克風輸入和藍牙耳機輸出。我的課程分類是並且必須是AVAudioSessionCategoryPlayAndRecord。我曾嘗試將setPreferredInput:用於內置麥克風,但它也將輸出切換至設備的揚聲器。通過藍牙耳機麥克風強制內置麥克風嗎?

我見過this(特別是評論),他們提到它是可能的。

回答

0

您可以使用此代碼

// 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);