我对使用 AudioQueue 服务有一点疑问。 我已经按照 Apple 网站上提供的指南进行操作,但是当我开始并运行音频队列时,我收到消息告诉我“AudioConverterNew 返回 -50”。 现在,我知道 -50 错误代码意味着存在错误参数。但是,我不知道哪个参数是错误的(非常感谢 Apple...)!
所以,这是我的代码。
这是我的类的参数,名为cPlayerCocoa
AudioQueueRef mQueue;
AudioQueueBufferRef mBuffers[NUMBER_BUFFERS]; // NUMBER_BUFFERS = 3
uint32 mBufferByteSize;
AudioStreamBasicDescription mDataFormat;
这是第一个函数:
static void
BuildBuffer( void* iAQData, AudioQueueRef iAQ, AudioQueueBufferRef iBuffer )
{
cPlayerCocoa* player = (cPlayerCocoa*) iAQData;
player->HandleOutputBuffer( iAQ, iBuffer );
}
它从包含 AudioQueue 的结构创建一个 cPlayerCocoa 并调用分配音频缓冲区的 HandleOutputBuffer 函数:
void
cPlayerCocoa::HandleOutputBuffer( AudioQueueRef iAQ, AudioQueueBufferRef iBuffer )
{
if( mContinue )
{
xassert( iBuffer->mAudioDataByteSize == 32768 );
int startSample = mPlaySampleCurrent;
int result = 0;
int samplecount = 32768 / ( mSoundData->BytesPerSample() ); // BytesPerSample, in my case, returns 4
tErrorCode error = mSoundData->ReadData( (int16*)(iBuffer->mAudioData), samplecount, &result, startSample );
AudioQueueEnqueueBuffer( mQueue, iBuffer, 0, 0 ); // I'm using CBR data (PCM), hence the 0 passed into the AudioQueueEnqueueBuffer.
if( result != samplecount )
mContinue = false;
startSample += result;
}
else
{
AudioQueueStop( mQueue, false );
}
}
在下一个函数中,AudioQueue 创建然后启动。 我开始初始化数据格式的参数。然后我创建 AudioQueue,并分配 3 个缓冲区。 分配缓冲区后,我启动 AudioQueue,然后运行循环。
void
cPlayerCocoa::ThreadEntry()
{
int samplecount = 32768 / ( mSoundData->BytesPerSample() );
mDataFormat.mSampleRate = mSoundData->SamplingRate(); // Returns 44100
mDataFormat.mFormatID = kAudioFormatLinearPCM;
mDataFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
mDataFormat.mBytesPerPacket = 32768;
mDataFormat.mFramesPerPacket = samplecount;
mDataFormat.mBytesPerFrame = mSoundData->BytesPerSample(); // BytesPerSample returns 4.
mDataFormat.mChannelsPerFrame = 2;
mDataFormat.mBitsPerChannel = uint32(mSoundData->BitsPerChannel());
mDataFormat.mReserved = 0;
AudioQueueNewOutput( &mDataFormat, BuildBuffer, this, CFRunLoopGetCurrent(), kCFRunLoopCommonModes, 0, &mQueue );
for( int i = 0; i < NUMBER_BUFFERS; ++i )
{
AudioQueueAllocateBuffer( mQueue, mBufferByteSize, &mBuffers[i] );
HandleOutputBuffer( mQueue, mBuffers[i] );
}
AudioQueueStart( mQueue, NULL ); // I want the queue to start playing immediately, so I pass NULL
do {
CFRunLoopRunInMode( kCFRunLoopDefaultMode, 0.25, false );
} while ( !NeedStopASAP() );
AudioQueueDispose( mQueue, true );
}
对 AudioQueueStart 的调用返回 -50(错误参数),我不知道出了什么问题... 我真的很感激一些帮助,提前致谢:-)
最佳答案
我认为您的 ASBD 值得怀疑。 PCM 格式具有可预测的 mBytesPerPacket
、mBytesPerFrame
和 mFramesPerPacket
值。对于普通的 16 位交错签名 44.1 立体声音频,ASBD 看起来像
AudioStreamBasicDescription asbd = {
.mFormatID = kAudioFormatLinearPCM,
.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
.mSampleRate = 44100,
.mChannelsPerFrame = 2,
.mBitsPerChannel = 16,
.mBytesPerPacket = 4,
.mFramesPerPacket = 1,
.mBytesPerFrame = 4,
.mReserved = 0
};
当其中一个 ASBD 不受支持时,AudioConverterNew
返回 -50。不存在 mBytesPerPacket
应为 32768 的 PCM 格式,这就是您收到错误的原因。
关于c++ - AudioConverterNew 返回 -50,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18631134/