OSStatus错误-50(无效参数)AudioQueueNewInput在iOS上录制音频

我一直在搜索互联网多年,试图找到这个错误的原因,但我被卡住了。 我一直在关注使用音频服务录制音频的Apple Developer文档,无论我做什么,我都会不断收到此错误。

我可以使用AVAudioRecorder将音频AVAudioRecorder成任何格式,但我的最终游戏是从输入数据中获取一个规范化的浮点数组,以便对其应用FFT(对于noob短语而言,我对音频编程很新)。

这是我的代码:

 - (void)beginRecording { // Initialise session [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; [[AVAudioSession sharedInstance] setActive:YES error:nil]; state.dataFormat.mFormatID = kAudioFormatLinearPCM; state.dataFormat.mSampleRate = 8000.0f; state.dataFormat.mChannelsPerFrame = 1; state.dataFormat.mBitsPerChannel = 16; state.dataFormat.mBytesPerPacket = state.dataFormat.mChannelsPerFrame * sizeof(SInt16); state.dataFormat.mFramesPerPacket = 1; //AudioFileTypeID fileID = kAudioFileAIFFType; state.dataFormat.mFormatFlags = kLinearPCMFormatFlagIsBigEndian | kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; OSStatus err = AudioQueueNewInput(&state.dataFormat, handleInputBuffer, &state, CFRunLoopGetMain(), kCFRunLoopCommonModes, 0, &state.queue); printf("%i", err); // this is always -50 ie invalid parameters error deriveBufferSize(state.queue, state.dataFormat, 0.5, &state.bufferByteState); for (int i = 0; i dataFormat.mBytesPerPacket != 0) { inNumPackets = inBuffer->mAudioDataByteSize / state->dataFormat.mBytesPerPacket; } printf("Called"); /* if (AudioFileWritePackets(state->audioFile, false, inBuffer->mAudioDataByteSize, inPacketDesc, state->currentPacket, &inNumPackets, inBuffer->mAudioData) == noErr) { state->currentPacket += inNumPackets; } */ if (state->isRunning) { AudioQueueEnqueueBuffer(state->queue, inBuffer, 0, NULL); } } void deriveBufferSize(AudioQueueRef audioQueue, AudioStreamBasicDescription ABSDescription, Float64 secs, UInt32 *outBufferSize) { static const int maxBufferSize = 0x50000; int maxPacketSize = ABSDescription.mBytesPerPacket; if (maxPacketSize == 0) { UInt32 maxVBRPacketSize = sizeof(maxPacketSize); AudioQueueGetProperty(audioQueue, kAudioConverterPropertyMaximumOutputPacketSize, &maxPacketSize, &maxVBRPacketSize); } Float64 numBytesForTime = ABSDescription.mSampleRate * maxPacketSize * secs; UInt32 x = (numBytesForTime < maxBufferSize ? numBytesForTime : maxBufferSize); *outBufferSize = x; } 

如果有人知道这里发生了什么,我将非常感激。 这是错误的苹果文档

你得到了-50( kAudio_ParamError ),因为你还没有初始化AudioStreamBasicDescriptionmBytesPerFrame字段:

 asbd.mBytesPerFrame = asbd.mFramesPerPacket*asbd.mBytesPerPacket; 

其中asbdstate.dataFormat 。 在你的情况下, mBytesPerFrame = 2

我也不会指定kLinearPCMFormatFlagIsBigEndian ,让记录器返回本机字节顺序样本。