1
我在iOS中使用以下代码将mp3文件转换为字节数组。但是,进入while循环时,它会产生err = -40(OSError = -40)。任何人都可以请帮忙。或者请让我知道我可以将mp3/wav文件转换为bytearray。参考 - How to convert WAV/CAF file's sample data to byte array?OSError -40将音频文件转换为字节数组iPhone
NSString *urlHere = [[NSBundle mainBundle] pathForResource:@"r2d2" ofType:@"mp3"];
CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:urlHere];
AudioFileID audioFile;
OSStatus err = AudioFileOpenURL(url, 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;
}
}
这个错误的意思是“无效的文件位置”为它的价值。 – borrrden
好的......那么,CFURLRef创建的问题是什么?我怎么可能使用AudioFileOpenURL。请帮忙。 – ScarletWitch
更具体地说明它何时发出该消息。循环的第一次迭代?最后一次迭代?中间? – borrrden