【发布时间】:2013-09-08 23:33:11
【问题描述】:
我有一点关于使用 AudioQueue 服务的问题。 我已按照 Apple 网站上提供的指南进行操作,但是当我开始并运行音频队列时,我收到一条消息,告诉我“AudioConverterNew 返回 -50”。 现在,我知道 -50 错误代码意味着存在错误参数。但是,我不知道哪个参数是坏的(非常感谢苹果......)!
所以,这是我的代码。
这是我的类的参数,命名为 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(错误参数),我不知道出了什么问题... 我真的很感激一些帮助,在此先感谢:-)
【问题讨论】:
-
您可以首先检查所有返回
OSStatus的调用的结果。 -
这个问题的标题说AudioConverterNew失败,但正文询问AudioQueueStart。 AudioConverterNew 的消息在哪里生成?
-
@molbdnilo 正如我已经解释过的,-50 返回“错误参数”。
-
@sbooth 在调用AudioQueueStart时生成。
-
@ThierryCantet 是的,但您永远无法确保您在此之前所做的一切都成功。 Apple 的指南故意排除了错误处理,但这只是为了保持示例简短。
标签: c++ objective-c macos core-audio