3

我对使用 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(错误参数),我不知道出了什么问题......我真的很感激一些帮助,在此先感谢 :-)

4

1 回答 1

3

我认为你的 ASBD 是可疑的。PCM 格式具有可预测的mBytesPerPacketmBytesPerFrame和值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
};

AudioConverterNew当其中一个 ASBD 不受支持时返回 -50。没有 PCM 格式mBytesPerPacket应该是 32768,这就是您收到错误的原因。

于 2013-09-05T23:13:59.797 回答