2013-06-24 2 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

이 오류는 가치가 무엇인지에 대해 "잘못된 파일 위치"를 의미 : 이미 포인터이기 때문에 당신이해야 할 모든은 pBuffer 변수 옆에있는 & 기호를 제거하는 것입니다. – borrrden

+1

Ok ... 그렇다면 CFURLRef에서 문제가 발생 했습니까? 그 밖에 어떻게 AudioFileOpenURL을 사용할 수 있습니까? 도와주세요. – ScarletWitch

+0

메시지를 보내는시기에 대해 더 구체적으로 설명하십시오. 루프의 첫 번째 반복? 마지막 반복? 중간? – borrrden

답변

1

나는 똑같은 문제가있었습니다.

err = AudioFileReadBytes(audioFile, true, offset, &toRead, pBuffer); 
관련 문제