OSError -40 while converting an audio file to Byte array iPhone

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-05 03:36:24

问题


I am using the following code in iOS to convert an mp3 file to byte array. However, on entering the while loop, it gives err = -40 (OSError = -40) . Can anyone please help. Or kindly let me know how i can convert an mp3/wav file to bytearray. Reference - How to convert WAV/CAF file's sample data to byte array?

 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;
    }
}

回答1:


I had the exact same problem. All you need to do is to remove the & sign next to the pBuffer variable, since it is already a pointer:

err = AudioFileReadBytes(audioFile, true, offset, &toRead, pBuffer);


来源:https://stackoverflow.com/questions/17270024/oserror-40-while-converting-an-audio-file-to-byte-array-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!