本文介绍了模拟器中的AudioQueue回调,但不在设备上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently working on an audio processing app on iPhone. it is based on Apple's SpeakHere sample code, aims at real-time audio processing and playback. The code works well in simulator, but not callback when tested on device.

回调函数如下:

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示例代码中的相同.

Other parts remains same as in SpeakHere sample code.

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

So what is the problem related to this strange behavior? Any insight will be appreciated. And kindly let me know if more information is needed.

非常感谢您.

干杯

Manca

推荐答案

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

You have to initialize the AudioSession and set its mode to 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!");

请参见 SpeakHereController.mm .

这篇关于模拟器中的AudioQueue回调,但不在设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:04