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

标签 ios core-audio exc-bad-access audioqueue

我遇到了一些非常奇怪的事情(至少对我来说是这样,但我是个菜鸟)。

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。 Apple writes

numBytesReadFromFile: On output, the number of bytes of audio data that were read from the audio file.

到目前为止一切顺利。 Apple 像上面的示例代码一样声明了 numBytesReadFromFile 但对我来说这行代码崩溃了!我收到 EXC BAD ACCESS。

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 并发生上述错误。如果我的假设是正确的,有人可以更详细地向我解释这个问题吗,因为我在多线程方面没有经验。

谢谢。

最佳答案

AudioFileReadPacketData 的参数ioNumBytes 是输入/输出参数。 documentation说:

On input, the size of the outBuffer parameter, in bytes. On output, the number of bytes actually read.

You will see a difference in the input and output values if the byte size for the number of packets you request in the ioNumPackets parameter is smaller than the buffer size you pass in the outBuffer parameter. In this case, the output value for this parameter is smaller than its input value.

调用函数时的值决定了将有多少数据写入您的缓冲区。如果您发布的代码是正确的,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
                                    );

关于ios - 为什么 AudioFileReadPacketData 的参数 ioNumBytes 会导致崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28443275/

相关文章:

iphone - 如何在 iOS 上使用 ExtAudioFile 使 AAC 编码正常工作?

linux - Linux 依赖项的 Mac Firebreath 插件端口

ios - [UILabel drawRect] 中的 EXC_BAD_ACCESS

c++ - boost::thread join 中的 EXC BAD ACCESS

iOS:通用链接始终打开应用程序

iphone - UINavigationController 嵌套在 moreViewController 中的问题

ios - 如何使用 Dropbox API 重新连接应用程序

core-audio - 带有 AVAudioEngine 的自定义 AVAudioUnit 在设置提供程序 block 上崩溃

iphone - 是否可以通过单击按钮进行调用并显示我们自己的 View 而不是默认 View ?

objective-c - 解决 Cocoa 中的 EXC_BAD_ACCESS 问题?