我从事自定义voip应用程序。它依赖于CoreAudio上的音频输入/输出。 我发现了一个问题 - 当应用程序调用AudioOutputUnitStart时 - 它会冻结几秒钟。
OSStatus status;
InitializeAudioSession();
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_RemoteIO;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
mVoiceUnitComponent = AudioComponentFindNext(NULL, &desc);
status = AudioComponentInstanceNew(mVoiceUnitComponent, &mVoiceUnit);
UInt32 enable = 1;
AudioUnitElement inputBus = 1, outputBus = 0;
status = AudioUnitSetProperty(mVoiceUnit, kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Input, inputBus,
&enable, sizeof(enable));
status = AudioUnitSetProperty(mVoiceUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output,
outputBus, &enable, sizeof(enable));
memset(&mStreamDesc, 0, sizeof(mStreamDesc));
mStreamDesc.mSampleRate = SPK_SAMPLE_RATE;
mStreamDesc.mFormatID = kAudioFormatLinearPCM;
mStreamDesc.mFormatFlags = kAudioFormatFlagsCanonical;
mStreamDesc.mBitsPerChannel = 16;
mStreamDesc.mChannelsPerFrame = 1;
mStreamDesc.mBytesPerFrame = 2;
mStreamDesc.mFramesPerPacket = 1;
mStreamDesc.mBytesPerPacket = 2;
status = AudioUnitSetProperty(mVoiceUnit, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output,
inputBus, &mStreamDesc, sizeof(mStreamDesc));
StartAudioSession();
status = AudioUnitInitialize(mVoiceUnit);
status = AudioUnitSetProperty(mVoiceUnit, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
outputBus, &mStreamDesc, sizeof(mStreamDesc));
AURenderCallbackStruct outputCallback;
outputCallback.inputProc = IOSAudio::OutputRenderCallback;
outputCallback.inputProcRefCon = this;
status = AudioUnitSetProperty(mVoiceUnit, kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input, outputBus,
&outputCallback, sizeof(outputCallback));
AURenderCallbackStruct inputCallback;
inputCallback.inputProc = IOSAudio::InputRenderCallback;
inputCallback.inputProcRefCon = this;
status = AudioUnitSetProperty(mVoiceUnit, kAudioOutputUnitProperty_SetInputCallback,
kAudioUnitScope_Global, inputBus,
&inputCallback, sizeof(inputCallback));
status = AudioOutputUnitStart(mVoiceUnit);
mActive = true;
SPK_SAMPLE_RATE是8000或32000 - 我测试了它们两个。
愿任何人给我建议吗?
答案 0 :(得分:0)
我做了一点调查。 看起来我错了 - 只有模拟器的问题。 我没有找到任何原因。 所以我添加了一个解决方法 - 在工作线程中打开音频。