1

iOS で次のコードを使用して、mp3 ファイルをバイト配列に変換しています。ただし、while ループに入ると、 err = -40 (OSError = -40) が返されます。誰でも助けてください。または、mp3/wav ファイルを bytearray に変換する方法を教えてください。参考 - WAV/CAFファイルのサンプルデータをバイト配列に変換するには?

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

1 に答える 1

1

私はまったく同じ問題を抱えていました。変数は既にポインターであるため&、変数の横にある記号を削除するだけです。pBuffer

err = AudioFileReadBytes(audioFile, true, offset, &toRead, pBuffer);
于 2013-10-25T18:22:23.123 に答える