为什么AudioFileReadPacketData的参数ioNumBytes会导致崩溃



我有一些非常奇怪的事情(至少对我来说,但我是个傻瓜)。

UInt32 numBytesReadFromFile;
OSStatus err = AudioFileReadPacketData(
                                    audioFile, // The audio file whose audio packets you want to read.
                                    NO, // is cache set?
                                    &numBytesReadFromFile, // On output, the number of bytes of audio data that were read from the audio file.
                                    (AudioStreamPacketDescription *)_packetDescriptions, // array of packet descriptions of data that was read from the audio file (for CBR null)
                                    currentPacket, // the next packet to be read into buffer
                                    &numPackets, // number of actually read buffers
                                    _audioQueueBuffer->mAudioData
                                    );

AudioFileReadPacketData从音频文件中读取数据并将其放入缓冲区。

所以我的问题是关于参数numBytesReadFromFile。苹果编写

numBytesReadFromFile:输出时,从音频文件中读取的音频数据的字节数。

到目前为止还不错。苹果像上面的示例代码一样声明numBytesReadFromFile,但对我来说,这行代码崩溃了!我有一个EXC坏访问权限。

UInt32 numBytesReadFromFile;

我需要像这样声明numBytesReadFromFile,一切都很好:

UInt32 numBytesReadFromFile = 2048; // 2048 = size of my buffer

然而,也崩溃了

UInt32 numBytesReadFromFile = 12
UInt32 numBytesReadFromFile = sizeof(UInt32)

但这不是

UInt32 numBytesReadFromFile = 1021; // a random number

我不是一个经验丰富的C程序员,但据我所知,我通过声明numBytesReadFromFile和方法audiofilereadpacketdata将其数据写入变量的地址来保留一些内存。如果我错了,请纠正我。

那它为什么会崩溃呢?我想我还没有解决真正的问题。

我的假设是我有某种多线程问题。当我准备队列时,我在主线程上调用AudioFileReadPacketData并声明

UInt32 numBytesReadFromFile;

工作良好。我开始播放音频,并调用了一个回调,该回调在音频队列的内部后台线程上调用AudioFilereadPacketData,出现了上述错误。如果我的假设是正确的,有人能更详细地解释我这个问题吗,因为我在多线程方面没有经验。

谢谢。

参数ioNumBytesAudioFileReadPacketData是一个输入/输出参数。文件上写着:

在输入时,outBuffer参数的大小,以字节为单位。在输出时,实际读取的字节数。

如果字节您在ioNumPackets中请求的数据包数量的大小参数小于在outBuffer中传递的缓冲区大小参数在这种情况下,此参数的输出值为小于其输入值。

调用函数时的值决定将有多少数据写入缓冲区。如果您发布的代码是正确的,则numBytesReadFromFile永远不会初始化为_audioQueueBuffer->mAudioData的大小,并且程序会崩溃,因为它试图向_audioQueueBuffer->mAudioData写入未确定数量的数据。尝试在函数调用之前设置参数:

UInt32 numBytesReadFromFile = _audioQueueBuffer->mAudioDataByteSize;
OSStatus err = AudioFileReadPacketData(
                                    audioFile, // The audio file whose audio packets you want to read.
                                    NO, // is cache set?
                                    &numBytesReadFromFile, // On output, the number of bytes of audio data that were read from the audio file.
                                    (AudioStreamPacketDescription *)_packetDescriptions, // array of packet descriptions of data that was read from the audio file (for CBR null)
                                    currentPacket, // the next packet to be read into buffer
                                    &numPackets, // number of actually read buffers
                                    _audioQueueBuffer->mAudioData
                                    );

相关内容

  • 没有找到相关文章

最新更新