AudioFileReadPackets错误-50

时间:2015-02-12 23:31:30

标签: macos compiler-errors core-audio audiotoolbox

我正在使用Audio API,我的第一步是将文件内容读入缓冲区。 我有以下代码,但是当执行AudioFileReadPackets时,我得到了返回代码-50但我没有得到它,因为在文档中它不存在。 你能指点我正确的方向吗?任何想法将不胜感激!

    //create url for file
    CFURLRef myFileURL = CFURLCreateWithString(kCFAllocatorDefault, INPUT_FILE, NULL);
    AudioFileID audioFile;
    OSStatus error;

    error = AudioFileOpenURL(myFileURL,kAudioFileReadPermission,0,&audioFile);
    CFRelease(myFileURL);

    if(error != noErr) {
        printf("%d",(int)error);
        exit(1);
    }

    AudioStreamBasicDescription  dataFormat;
    UInt32 size = sizeof(dataFormat);

    error = AudioFileGetProperty(audioFile, kAudioFilePropertyDataFormat, &size, &dataFormat);

    if(error != noErr) {
        printf("%d",(int)error);
        exit(1);
    }

    UInt32 dataSize;
    error = AudioFileGetPropertyInfo(audioFile, kAudioFilePropertyAudioDataPacketCount, &dataSize, NULL);

    if(error != noErr) {
        printf("%d",(int)error);
        exit(1);
    }

    UInt32 packetCount;
    error = AudioFileGetProperty(audioFile, kAudioFilePropertyAudioDataPacketCount, &dataSize, &packetCount);

    if(error != noErr) {
        printf("%d",(int)error);
        exit(1);
    }

    printf("File opened, packet count: %d\n", packetCount);
    UInt32 packetsRead = packetCount;
    UInt32 bytesRead = 0;

    if(packetCount > 0) {
        SInt16 audioData = *(SInt16 *)malloc(2 * packetCount);
        error = AudioFileReadPackets(audioFile, false, &bytesRead, NULL, 0, &packetsRead, audioData);

        if(error != noErr) {
            printf("%d",(int)error);
            exit(1);
        }

        printf("Read %d bytes, %d packets",bytesRead,packetsRead);

    }

1 个答案:

答案 0 :(得分:1)

-50 = paramErr表示您传递的参数不正确。文档说明,对于恒定比特率音频,AudioFileReadPackets的第四个参数必须只能为NULL。