1

我目前正在 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 示例代码相同。

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

非常感谢。

干杯,

曼卡

4

1 回答 1

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!");

参见著名awakeFromNibSpeakHere示例中的 SpeakHereController.mm。

于 2012-04-10T09:49:03.070 回答