我有以下代码用于将WAV文件的内容读取到SInt16
数组中:
AudioBufferList *buffers;
UInt32 ablSize = offsetof(AudioBufferList, mBuffers) +
(sizeof(AudioBuffer) * 1);
buffers = malloc(ablSize);
buffers->mNumberBuffers = 1;
buffers->mBuffers[0].mNumberChannels = 1;
buffers->mBuffers[0].mDataByteSize = dataByteSize;
UInt32 dataSize = (UInt32)fileLengthFrames * sizeof(SInt16);
self.extractedSamples = malloc(dataSize);
self.extractedByteCount = dataByteSize;
UInt32 totalFramesRead = 0;
do {
UInt32 framesRead = (UInt32)fileLengthFrames - totalFramesRead;
buffers->mBuffers[0].mData = self.extractedSamples +
(totalFramesRead * sizeof(SInt16));
ExtAudioFileRead(eaf, &framesRead, buffers);
totalFramesRead += framesRead;
} while (totalFramesRead < fileLengthFrames);
free(buffers);
对于持续时间少于0.5秒的文件,此方法工作正常。但是对于我正在测试的较长文件,该应用程序崩溃,并在
do
循环内出现了严重的访问错误。对于此文件,dataByteSize
是60472,并且在循环开始时buffer->mBuffers[0].mDataByteSize
也是60472。但是当崩溃发生时,我看到buffer->mBuffers[0].mDataByteSize
更改为57300,这大概就是为什么现在崩溃的原因。任何人都知道在循环中间如何/为什么更改此值?我的一个猜测是我没有正确保留
AudioBufferList
,并且mDataByteSize
的内存空间以某种方式被覆盖。编辑:当此代码在模拟器上以相同文件运行时,它可以正常工作。
最佳答案
在每次调用ExtAudioFileRead之前,应将mDataByteSize设置为frameRead * sizeof(SInt16)* channelCount
关于ios - 试图了解为什么AudioBuffer的mDataByteSize属性正在更改,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30171498/