- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了一些非常奇怪的事情(至少对我来说是这样,但我是个菜鸟)。
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/
我遇到了一些非常奇怪的事情(至少对我来说是这样,但我是个菜鸟)。 UInt32 numBytesReadFromFile; OSStatus err = AudioFileReadPacketData
我是一名优秀的程序员,十分优秀!