iphone - 使用AudioFileReadPackets通过Audio Queue访问原始音频数据

标签 iphone c++ audio audioqueueservices

我正在尝试访问发送到Audio Queue回调的原始音频数据,但是在调用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,但是我从this问题中找到了一个可行的解决方案。

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

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

关于iphone - 使用AudioFileReadPackets通过Audio Queue访问原始音频数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11266489/

相关文章:

iphone - UITableView 和 UIScrollview 停止 cocos2d 动画

c++ - 尝试退出循环时 Ctrl+Z 不起作用

c++ - 当它绑定(bind)到调用函数中的 const 引用时,它的返回值的生命周期如何扩展到调用函数的范围?

java - 如何录制声音并将其设置为Java文件?

iphone - iOS-如何计算UITableViewCell的默认textLabel的宽度?

iphone - 如何使用[[UIApplication共享应用程序] openURL :] open other app?

iphone - 是否有任何私有(private) api 可以监控 iphone 上的键盘访问(哪个应用程序正在使用键盘,它的访问时间等)?

c++ - 如何在C++中生成随机数?

javascript - 控制 YouTube 视频播放器中的左/右音频 channel 音量

Android MP3 MediaPlayer 使某些手机崩溃