AudioQueue在模拟器中回调但不在设备上回调

时间:2011-03-07 10:10:54

标签: iphone objective-c ios core-audio audioqueueservices

我目前正在开发iPhone上的音频处理应用程序。它基于Apple的SpeakHere示例代码,旨在实时音频处理和回放。该代码在模拟器中运行良好,但在设备上进行测试时无法回调。

回调函数是这样的:

void AQPlayer::AQBufferCallback(void *                  inUserData,
                            AudioQueueRef           inAQ,
                            AudioQueueBufferRef     inCompleteAQBuffer) 
{
AQPlayer *THIS = (AQPlayer *)inUserData;

if (THIS->mIsDone) return;

UInt32 numBytes;
UInt32 nPackets = THIS->GetNumPacketsToRead();
OSStatus result = AudioFileReadPackets(THIS->GetAudioFileID(), false, &numBytes, inCompleteAQBuffer->mPacketDescriptions, THIS->GetCurrentPacket(), &nPackets, 
                                       inCompleteAQBuffer->mAudioData);
if (result)
    printf("AudioFileReadPackets failed: %d", (int)result);
if (nPackets > 0) {
    inCompleteAQBuffer->mAudioDataByteSize = numBytes;      
    inCompleteAQBuffer->mPacketDescriptionCount = nPackets;     

    //Buffer Modification

    SInt16 *testBuffer = (SInt16*)inCompleteAQBuffer->mAudioData;       

    for (int i = inCompleteAQBuffer->mAudioDataByteSize/sizeof(SInt16); i > 0; i --)
    {           
        //printf("before modification %d", (int)*testBuffer);                                               
        *testBuffer = *testBuffer/2;   //Simplest processing
        //printf("after modification %d", (int)*testBuffer);
        testBuffer++;           
    }               
    //Enqueue Buffer
    AudioQueueEnqueueBuffer(inAQ, inCompleteAQBuffer, 0, NULL);

    THIS->mCurrentPacket = (THIS->GetCurrentPacket() + nPackets);
} 

其他部分与SpeakHere示例代码中的相同。

那么与这种奇怪行为有关的问题是什么?任何见解将不胜感激。如果需要更多信息,请告诉我。

非常感谢。

干杯,

曼卡

1 个答案:

答案 0 :(得分:0)

您必须初始化AudioSession并将其模式设置为Play& Record:

AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 category = kAudioSessionCategory_PlayAndRecord;  
int error = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
if (error) printf("Couldn't set audio category!");

请参阅着名的SpeakHere示例中的SpeakHereController.mm中的awakeFromNib