2013-06-24 93 views
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; 
    } 
} 
+1

這個錯誤的意思是「無效的文件位置」爲它的價值。 – borrrden

+1

好的......那麼,CFURLRef創建的問題是什麼?我怎麼可能使用AudioFileOpenURL。請幫忙。 – ScarletWitch

+0

更具體地說明它何時發出該消息。循環的第一次迭代?最後一次迭代?中間? – borrrden

回答

1

我有完全相同的問題。 所有你需要做的是去除旁邊pBuffer變量&標誌,因爲它已經是一個指針:

err = AudioFileReadBytes(audioFile, true, offset, &toRead, pBuffer); 
相關問題