在iOS中启用蓝牙以进行PJSIP语音通话 [英] Enable Bluetooth for pjsip voice call in ios
问题描述
我正在制作VOIP应用程序,现在我们需要用蓝牙连接语音电话. 我们尝试将通话与蓝牙连接,但听不到任何声音.
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"));
}
}
在 pjmedia-audiodev-> coreaudio_dev.m 文件中添加了以上代码.
Above code added in pjmedia-audiodev->coreaudio_dev.m file.
还尝试了以下代码:-
+(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);
}
代码已执行,但无法使用蓝牙听到声音. 如果有人想为 pjsip 启用蓝牙,请给我.
Code execute but not able to hear voice using Bluetooth. If anyone have an idea of enabling Bluetooth for pjsip then please give me.
谢谢.
推荐答案
设置 AVAudioSessionCategory 和 port 后,将preferredInput设置为蓝牙端口,以便将蓝牙设置为活性.请遵循以下代码并调用switchBluetooth
方法.
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;
}
这篇关于在iOS中启用蓝牙以进行PJSIP语音通话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!