How to switch between an audio input source (Bluetooth, built-in microphone) using AVFoundation

I am currently having problems switching the audio input source between the built-in microphone and the Bluetooth microphone, iOS8

I tried to find online solutions, but received nothing :(

Someone please advise me on the right way to achieve.

Looking forward to your help!

+2
ios bluetooth avfoundation
Dec 08 '15 at 17:24
source share
1 answer

I have this code.

bluetoothInput is just a logical way to switch between a bluetooth microphone and a regular microphone.

-(void) changeBluetoothInput{ if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) { if(self.bluetoothInput){ //[[AVAudioSession sharedInstance] setActive:NO error:nil]; [[AVAudioSession sharedInstance] setActive:YES error:nil]; AVAudioSessionPortDescription* _bluetoothPort = [self bluetoothAudioDevice]; [[AVAudioSession sharedInstance] setPreferredInput:_bluetoothPort error:nil]; }else{ //[[AVAudioSession sharedInstance] setActive:NO error:nil]; //[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; [[AVAudioSession sharedInstance] setActive:YES error:nil]; AVAudioSessionPortDescription* _bluetoothPort = [self normalAudioDevice]; [[AVAudioSession sharedInstance] setPreferredInput:_bluetoothPort error:nil]; } } 

}

 - (AVAudioSessionPortDescription*)bluetoothAudioDevice { NSArray* bluetoothRoutes = @[AVAudioSessionPortBluetoothA2DP, AVAudioSessionPortBluetoothLE, AVAudioSessionPortBluetoothHFP]; return [self audioDeviceFromTypes:bluetoothRoutes]; } - (AVAudioSessionPortDescription*)normalAudioDevice { NSArray* bluetoothRoutes = @[AVAudioSessionPortBuiltInMic]; return [self audioDeviceFromTypes:bluetoothRoutes]; } - (AVAudioSessionPortDescription*)audioDeviceFromTypes:(NSArray*)types { NSArray* routes = [[AVAudioSession sharedInstance] availableInputs]; for (AVAudioSessionPortDescription* route in routes) { if ([types containsObject:route.portType]) { return route; } } return nil; } 
+1
Dec 08 '15 at 17:27
source share



All Articles