问题
I am making VOIP app and now we required to connect voice calls with Bluetooth. We tried to connect the call with Bluetooth but not hear anything.
/* Initialize audio session category and mode */
{
AVAudioSession *sess = [AVAudioSession sharedInstance];
pj_bool_t err;
if ([sess respondsToSelector:@selector(setCategory:withOptions:error:)])
{
err = [sess setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionAllowBluetooth
error:nil] != YES;
} else {
err = [sess setCategory:AVAudioSessionCategoryPlayAndRecord
error:nil] != YES;
}
if (err) {
PJ_LOG(3, (THIS_FILE,
"Warning: failed settting audio session category"));
}
if ([sess respondsToSelector:@selector(setMode:error:)] &&
[sess setMode:AVAudioSessionModeVoiceChat error:nil] != YES)
{
PJ_LOG(3, (THIS_FILE, "Warning: failed settting audio mode"));
}
}
Above code added in pjmedia-audiodev->coreaudio_dev.m file.
Also tried below code :-
+(void)EnableBluethooth
{
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory: AVAudioSessionCategoryPlayAndRecord
error:
nil];
[audioSession setActive: YES error: nil];
UInt32 allowBluetoothInput = 1;
OSStatus ostatus = AudioSessionSetProperty (
kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
sizeof(allowBluetoothInput), &allowBluetoothInput);
pjmedia_aud_dev_route route = PJMEDIA_AUD_DEV_ROUTE_BLUETOOTH;
pj_status_t status =
pjsua_snd_set_setting(PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, &route,
PJ_TRUE);
NSLog(@"statuys is--->%d",status);
NSLog(@"status = %x", ostatus);
}
+(void)DisableBluethooth
{
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory: AVAudioSessionCategoryPlayAndRecord
error: nil];
[audioSession setActive: NO error: nil];
pjmedia_aud_dev_route route = PJMEDIA_AUD_DEV_ROUTE_BLUETOOTH;
pjsua_snd_set_setting(PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, &route,
PJ_FALSE);
}
Code execute but not able to hear voice using Bluetooth. If anyone have an idea of enabling Bluetooth for pjsip then please give me.
Thank you.
回答1:
After setting the AVAudioSessionCategory and port, set the preferredInput as bluetooth port, so the bluetooth will be activated. Please follow the below code and call switchBluetooth
method.
- (AVAudioSessionPortDescription*)builtinAudioDevice
{
NSArray* builtinRoutes = @[AVAudioSessionPortBuiltInMic];
return [self audioDeviceFromTypes:builtinRoutes];
}
- (AVAudioSessionPortDescription*)bluetoothAudioDevice
{
NSArray* bluetoothRoutes = @[AVAudioSessionPortBluetoothA2DP, AVAudioSessionPortBluetoothLE, AVAudioSessionPortBluetoothHFP];
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;
}
- (BOOL)switchBluetooth:(BOOL)onOrOff
{
NSError* audioError = nil;
BOOL changeResult = NO;
if (onOrOff == YES)
{
AVAudioSessionPortDescription *bluetoothPort = [self bluetoothAudioDevice];
if (bluetoothPort) {
changeResult = [[AVAudioSession sharedInstance] setPreferredInput:bluetoothPort
error:&audioError];
}
}
else
{
AVAudioSessionPortDescription* builtinPort = [self builtinAudioDevice];
if (builtinPort) {
changeResult = [[AVAudioSession sharedInstance] setPreferredInput:builtinPort
error:&audioError];
}
}
if (audioError) {
NSLog(@"BluetoothActiveError: %@ suggestion : %@",audioError.localizedDescription,audioError.localizedRecoverySuggestion);
}
return changeResult;
}
来源:https://stackoverflow.com/questions/51396267/enable-bluetooth-for-pjsip-voice-call-in-ios