使用AvauioSession与iOS之间的多个蓝牙设备之间的音频输出路由音频输出
获得了一个视频会议应用程序,在耳机,扬声器和连接的蓝牙设备之间实现了设备选择功能。除了在蓝牙设备本身之间切换外,所有这些功能似乎都适当。
由于某种原因,只有最后一个连接的设备可以使用setPreferredInput
,即使在Avaudiosession sharedInstance的可用Inputs中可用,您也无法切换到其他设备。
我尝试搜索解决方案,但找到了5年前的同样未解决的问题我尝试过,我尝试过,我尝试过与更改setActive呼叫相同,但也没有成功。
以下是测试代码,在这里:
AVAudioSession *_audioSession = [AVAudioSession sharedInstance];
AVAudioSessionCategoryOptions _incallAudioCategoryOptionsAll = AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionAllowAirPlay;
[_audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:_incallAudioCategoryOptionsAll
error:nil];
[_audioSession setMode:AVAudioSessionModeVoiceChat error: nil];
RCT_EXPORT_METHOD(setAudioDevice:(NSString *)device
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
BOOL success;
NSError *error = nil;
NSLog(@"[setAudioDevice] - Attempting to set audiodevice as: %@", device);
if ([device isEqualToString:kDeviceTypeSpeaker]) {
success = [_audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
} else {
AVAudioSessionPortDescription *port = nil;
for (AVAudioSessionPortDescription *portDesc in _audioSession.availableInputs) {
if ([portDesc.UID isEqualToString:device]) {
port = portDesc;
break;
}
}
if (port != nil) {
[_audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil];
success = [_audioSession setPreferredInput:port error:&error];
if(error != nil)
{
NSLog(@"setAudioDevice %@ %@", error.localizedDescription, error);
}
} else {
success = NO;
error = RCTErrorWithMessage(@"Could not find audio device");
}
}
if (success) {
resolve(@"setAudioDevice success!");
NSLog(@"resolved success");
} else {
reject(@"setAudioDevice", error != nil ? error.localizedDescription : @"", error);
NSLog(@"sent reject");
}
}
那么,如何使我们能够成功地从一个蓝牙设备更改为另一个蓝牙?
Got a video conference app, implementing device selection feature of switching between Earpiece, Speaker, and connected Bluetooth devices. All seems to function appropriately apart from switching between bluetooth devices themselves.
For some reason, only the last connected device gets the audio routed to it, and you can't switch back to the other ones even if they're available in availableInputs of AVAudioSession SharedInstance, using setPreferredInput
with OverridePortNone
I tried searching for resolutions but found the same unanswered issue from 5 years ago, I tried doing the same as changing setActive calls but was also unsuccessful.
Following is the test code, which is taken from here:
AVAudioSession *_audioSession = [AVAudioSession sharedInstance];
AVAudioSessionCategoryOptions _incallAudioCategoryOptionsAll = AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionAllowAirPlay;
[_audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:_incallAudioCategoryOptionsAll
error:nil];
[_audioSession setMode:AVAudioSessionModeVoiceChat error: nil];
RCT_EXPORT_METHOD(setAudioDevice:(NSString *)device
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
BOOL success;
NSError *error = nil;
NSLog(@"[setAudioDevice] - Attempting to set audiodevice as: %@", device);
if ([device isEqualToString:kDeviceTypeSpeaker]) {
success = [_audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
} else {
AVAudioSessionPortDescription *port = nil;
for (AVAudioSessionPortDescription *portDesc in _audioSession.availableInputs) {
if ([portDesc.UID isEqualToString:device]) {
port = portDesc;
break;
}
}
if (port != nil) {
[_audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil];
success = [_audioSession setPreferredInput:port error:&error];
if(error != nil)
{
NSLog(@"setAudioDevice %@ %@", error.localizedDescription, error);
}
} else {
success = NO;
error = RCTErrorWithMessage(@"Could not find audio device");
}
}
if (success) {
resolve(@"setAudioDevice success!");
NSLog(@"resolved success");
} else {
reject(@"setAudioDevice", error != nil ? error.localizedDescription : @"", error);
NSLog(@"sent reject");
}
}
So how can I make it that we're able to successfully change from one bluetooth device to other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论