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

时间:2014-09-05 05:23:18

标签: ios audio bluetooth core-audio audiounit

我正在做一个需要语音处理io音频单元做一些回音消除工作的应用程序。它工作正常,但事实证明,音频输出无法路由到蓝牙设备(它不是免提蓝牙设备,而是蓝牙立体声扬声器)。以下是我发起音频的方法

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);

我也将输出路由到扬声器而不是默认的耳机,音量非常低。但是,我无法实现将输出路由到蓝牙。有人可以帮我吗?感谢。

1 个答案:

答案 0 :(得分:1)

所以它真的取决于蓝牙设备的类型,是 BluetoothHFP (输入和输出), BluetoothA2DP (仅输出)还是 BluetoothLE < / strong>(仅输出)。如果仅输出设备,则无法在kAudioSessionCategory_PlayAndRecord类别中连接和路由音频。