AudioQueue callback in simulator but not on device

耗尽温柔 提交于 2019-12-24 11:49:16

问题


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.

The callback function is like this:

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);
} 

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.

Thank you very much.

Cheers,

Manca


回答1:


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

See awakeFromNib in SpeakHereController.mm from the famous SpeakHere example.



来源:https://stackoverflow.com/questions/5218335/audioqueue-callback-in-simulator-but-not-on-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!