How to get the bit rate of existing MP3 or AAC in iOS?

1.2k Views Asked by At

I want to display the bitrate of existing MP3 files and AAC files to the user, but I don't know how to correctly extract the bitrate of these files.

So far, I've tried to get the AudioConverterRef pointer of the ExtAudioFileRef and then I wanted to get the bitrate out of the converter, but my problem starts already in the first step. I get NULL for the kExtAudioFileProperty_AudioConverter property:

    OSStatus status;
    AudioConverterRef result = NULL;

    UInt32 size = sizeof(result);
    status = ExtAudioFileGetProperty(fileRef, kExtAudioFileProperty_AudioConverter, &size, &result);

    assert(status == noErr);

    assert(result != NULL); // here it fails

I can read the AudioStreamBasicDescription from the same fileRef successfully, so the fileRef is fine.

How to get the bitrate of compressed audio files?

1

There are 1 best solutions below

0
On

You can do it through the AudioFileID of an ExtAudioFileRef:

- (void) someMethod {
    ExtAudioFileRef extAudioFileRef = ...;  // init extAudioFileRef in some way

    AudioFileID audioFileId = [self getAudioFileID:extAudioFileRef];;
    UInt32 bitRate = [self getBitRate:audioFileId];
}

- (AudioFileID) getAudioFileID:(ExtAudioFileRef)fileRef {
    OSStatus status;
    AudioFileID result = NULL;

    UInt32 size = sizeof(result);
    status = ExtAudioFileGetProperty(fileRef, kExtAudioFileProperty_AudioFile, &size, &result);
    assert(status == noErr);

    return result;
}

- (UInt32) getBitRate:(AudioFileID)audioFileId {
    OSStatus status;
    UInt32 result = 0;

    UInt32 size = sizeof(result);
    status = AudioFileGetProperty(audioFileId, kAudioFilePropertyBitRate, &size, &result);
    assert(status == noErr);

    return result;
}