我有一些很奇怪的东西(至少对我来说是这样,但我是个菜鸟)。
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,但是对我来说,这一行代码崩溃了!我的权限很差。
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,然后发生上述错误。如果我的假设是正确的,是否有人能更详细地解释我的问题,因为我没有经验的多线程。
谢谢。
发布于 2015-02-11 13:51:33
参数ioNumBytes
to AudioFileReadPacketData
是一个in/out参数。文档说:
在输入时,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
);
https://stackoverflow.com/questions/28443275
复制相似问题