2

我有一个 .aif 文件,我尝试读取并添加到 char* 缓冲区。第一次阅读后,我得到 -38 错误。这是什么意思以及如何解决这个问题?这是我的代码:

NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *filename = [NSString stringWithFormat:@"test-1"];
    NSString *path = [mainBundle pathForResource:filename ofType:@"aif"];
    if (!path)
        return;

    NSURL *aFileURL = [NSURL fileURLWithPath:path isDirectory:NO];

    AudioFileID audioFile;

    OSStatus err = AudioFileOpenURL((__bridge CFURLRef)(aFileURL), 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); //!! I GET ERROR HERE
        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;
        }
    }
//work with audioBuffer...
4

0 回答 0