像示例项目MixerHost一样,我想将混合输出流写入文件而不是扬声器。所以我使用了 kAudioUnitSubType_GenericOutput 并调用:
CheckError(AudioUnitInitialize(mixerUnit), "AudioUnitInitialize mixerUnit");
CheckError(AudioUnitInitialize(ioUnit), "AudioUnitInitialize iOUnit");
CheckError(AudioOutputUnitStart(ioUnit), "AudioOutputUnitStart iOUnit");
AudioUnitRenderActionFlags ioActionFlags = 0;
AudioTimeStamp inTimeStamp;
memset(&inTimeStamp, 0, sizeof(AudioTimeStamp));
inTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
UInt32 inOutputBusNumber = 0;
UInt32 inNumberFrames = 100;
AudioBuffer buffer;
buffer.mNumberChannels = 2;
buffer.mDataByteSize = inNumberFrames*2;
buffer.mData = malloc(inNumberFrames*2);
AudioBufferList ioData;
ioData.mNumberBuffers = 1;
ioData.mBuffers[0] = buffer;
CheckError(AudioUnitRender(ioUnit,
&ioActionFlags,
&inTimeStamp,
inOutputBusNumber,
inNumberFrames,
&ioData),
"AudioUnitRender");
错误:AudioUnitRender (-50),请告诉我调用 AudioUnitRender() 的正确方法
iluvcapra 是对的,我改变了它并且它有效:
AudioUnitRenderActionFlags flags = 0;
AudioTimeStamp inTimeStamp;
memset(&inTimeStamp, 0, sizeof(AudioTimeStamp));
inTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
UInt32 busNumber = 0;
UInt32 numberFrames = 100;
int channelCount = 2;
AudioBufferList *bufferList = (AudioBufferList*)malloc(sizeof(AudioBufferList)+sizeof(AudioBuffer)*(channelCount-1));
bufferList->mNumberBuffers = channelCount;
for (int i=0; i<channelCount; i++) {
AudioBuffer buffer = {0};
buffer.mNumberChannels = 1;
buffer.mDataByteSize = numberFrames*sizeof(AudioUnitSampleType);
buffer.mData = calloc(numberFrames, sizeof(AudioUnitSampleType));
bufferList->mBuffers[i] = buffer;
}
CheckError(AudioUnitRender(ioUnit,
&flags,
&inTimeStamp,
busNumber,
numberFrames,
bufferList),
"AudioUnitRender ioUnit");