简体   繁体   English

有没有办法将语音处理io音频单元路由到蓝牙设备?

[英]Is there a way to route the voice processing io audio unit to a bluetooth device?

I am doing an app which needs the voice processing io audio unit to do some echo cancellation work. 我正在做一个需要语音处理io音频单元来做回声消除工作的应用程序。 It works fine, but it turned out that the audio output cannot route to bluetooth device (it is not a hands free bluetooth device but a bluetooth stereo speaker). 它工作正常,但事实证明音频输出无法路由到蓝牙设备(它不是免提蓝牙设备,而是蓝牙立体声扬声器)。 The following is how I initiate the audio stuff 以下是我如何启动音频的东西

AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_VoiceProcessingIO;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;

AudioComponent comp = AudioComponentFindNext(NULL, &desc);
AudioComponentInstanceNew(comp, &_audioUnit);

UInt32 one = 1;

AURenderCallbackStruct callbackStruct;
callbackStruct.inputProc = recordingCallback;
callbackStruct.inputProcRefCon = (__bridge void *)self;

AudioUnitSetProperty(_audioUnit, kAudioOutputUnitProperty_SetInputCallback,
                                 kAudioUnitScope_Global,
                                 kInputBus,
                                 &callbackStruct,
                                 sizeof(callbackStruct));

_audioFormat.mSampleRate = kSampleRate;
_audioFormat.mFormatID = kAudioFormatLinearPCM;
_audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
_audioFormat.mFramesPerPacket = 1;
_audioFormat.mChannelsPerFrame = 1;
_audioFormat.mBitsPerChannel = 16;
_audioFormat.mBytesPerPacket = 2;
_audioFormat.mBytesPerFrame = 2;

AudioUnitSetProperty(_audioUnit, kAudioUnitProperty_StreamFormat,
                                 kAudioUnitScope_Output,
                                 kInputBus,
                                 &_audioFormat,
                                 sizeof(_audioFormat));
AudioUnitSetProperty(_audioUnit, kAudioUnitProperty_StreamFormat,
                                 kAudioUnitScope_Input,
                                 kOutputBus,
                                 &_audioFormat,
                                 sizeof(_audioFormat));
AudioUnitSetProperty(_audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &one, sizeof(one));
AudioUnitSetProperty(_audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &one, sizeof(one));


// Configure the audio session
AVAudioSession *sessionInstance = [AVAudioSession sharedInstance];
[sessionInstance setCategory:AVAudioSessionCategoryPlayAndRecord
                 withOptions:AVAudioSessionCategoryOptionDuckOthers | AVAudioSessionCategoryOptionAllowBluetooth
                       error:NULL];

[sessionInstance overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleRouteChange:)
                                             name:AVAudioSessionRouteChangeNotification
                                           object:nil];

[[AVAudioSession sharedInstance] setActive:YES error:NULL];
AudioUnitInitialize(_audioUnit);
AudioOutputUnitStart(_audioUnit);

I also route the output to the speaker instead of the default ear speaker, which volume is very low. 我还将输出路由到扬声器,而不是默认的扬声器,该扬声器的音量很小。 However, I failed to achieve route the output to bluetooth. 但是,我无法实现将输出路由到蓝牙。 Could anyone help me on this? 有人可以帮我吗? Thanks. 谢谢。

So it really depends on the type of bluetooth device, whether it is BluetoothHFP (input & output), BluetoothA2DP (output only) or BluetoothLE (output only). 因此,实际上取决于蓝牙设备的类型,它是BluetoothHFP (输入和输出), BluetoothA2DP (仅输出)还是BluetoothLE (仅输出)。 If the device is output only, you won't be able to connect and route the audio in kAudioSessionCategory_PlayAndRecord category. 如果仅输出设备,则将无法连接和路由kAudioSessionCategory_PlayAndRecord类别中的音频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM