Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用AudioFileReadPackets通过音频队列访问原始音频数据_Iphone_C++_Audio_Audioqueueservices - Fatal编程技术网

Iphone 使用AudioFileReadPackets通过音频队列访问原始音频数据

Iphone 使用AudioFileReadPackets通过音频队列访问原始音频数据,iphone,c++,audio,audioqueueservices,Iphone,C++,Audio,Audioqueueservices,我试图访问发送到音频队列回调的原始音频数据,但调用AudioFileReadPackets后,字节数numBytes为0 void AQRecorder::MyInputBufferHandler(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer, const AudioTimeStamp * inStartTime, UInt32 inNumPackets, const AudioStreamPacketDes

我试图访问发送到音频队列回调的原始音频数据,但调用AudioFileReadPackets后,字节数numBytes为0

void AQRecorder::MyInputBufferHandler(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer, const AudioTimeStamp * inStartTime, UInt32 inNumPackets, const AudioStreamPacketDescription* inPacketDesc) {

AQRecorder *aqr = (AQRecorder *)inUserData;

try {
    if (inNumPackets > 0) {

        //read packets
        UInt32 numBytes;

        OSStatus result = AudioFileReadPackets(aqr->mRecordFile,      // The audio file from which packets of audio data are to be read.
                                               FALSE,                   // Set to true to cache the data. Otherwise, set to false.
                                               &numBytes,               // On output, a pointer to the number of bytes actually returned.
                                               (__bridge AudioStreamPacketDescription*)inPacketDesc,    // A pointer to an array of packet descriptions that have been allocated.
                                               aqr->mRecordPacket,  // The packet index of the first packet you want to be returned.
                                               &inNumPackets,               // On input, a pointer to the number of packets to read. On output, the number of packets actually read.                                           
                                               inBuffer->mAudioData); // A pointer to user-allocated memory.

        inBuffer->mAudioDataByteSize = numBytes;       
        SInt16 *testBuffer = (SInt16*)inBuffer->mAudioData;

        for (int i=0; i < numBytes; i++)
        {
            UInt16 currentData = testBuffer[i];
            printf("Current data in testbuffer is %d", currentData);
        }

    // if we're not stopping, re-enqueue the buffe so that it gets filled again
    if (aqr->IsRunning())
        XThrowIfError(AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL), "AudioQueueEnqueueBuffer failed");
} catch (CAXException e) {
    char buf[256];
    fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf));
}
}

当我只打印testBuffer的第一个索引时,我能够得到0到4294967296之间的值,所以它看起来不是空的。你知道为什么在AudioFileReadPackets执行后numBytes是0吗?

我仍然不确定为什么numBytes是0,但我从这个问题中找到了一个可行的解决方案

为了获得testBuffer的大小,我使用了以下代码:

int sampleCount = inBuffer->mAudioDataBytesCapacity / sizeof(SInt16);