更新时间:2023-11-15 18:49:34
假设你在iOS或OS X,你想要的AudioToolbox框架,特别是在API AudioFile.h
(或 ExtAudioFile.h
如果您需要将音频数据转换为在读取另一种格式。)
Assuming you're on iOS or OS X, you want the AudioToolbox framework, specifically the APIs in AudioFile.h
(or ExtAudioFile.h
if you need to convert the audio data to another format on read.)
例如,
#include <AudioToolbox/AudioFile.h>
...
AudioFileID audioFile;
OSStatus err = AudioFileOpenURL(fileURL, kAudioFileReadPermission, 0, &audioFile);
// get the number of audio data bytes
UInt64 numBytes = 0;
UInt32 dataSize = sizeof(numBytes);
err = AudioFileGetProperty(audioFile, kAudioFilePropertyAudioDataByteCount, &dataSize, &numBytes);
unsigned char *audioBuffer = (unsigned char *)malloc(numBytes);
UInt32 toRead = numBytes;
UInt64 offset = 0;
unsigned char *pBuffer = audioBuffer;
while(true) {
err = AudioFileReadBytes(audioFile, true, offset, &toRead, &pBuffer);
if (kAudioFileEndOfFileError == err) {
// cool, we're at the end of the file
break;
} else if (noErr != err) {
// uh-oh, some error other than eof
break;
}
// advance the next read offset
offset += toRead;
// advance the read buffer's pointer
pBuffer += toRead;
toRead = numBytes - offset;
if (0 == toRead) {
// got to the end of file but no eof err
break;
}
}
// Process audioBuffer ...
free(audioBuffer);