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

那么,与这种奇怪行为相关的问题又是什么呢?任何见识将不胜感激。并请让我知道是否需要更多信息。

非常感谢你。

干杯,

曼卡

最佳答案

您必须初始化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

07-27 15:49