Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to check metadata of an audio file in iOS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 561
    Comment on it

    Hi Readers,

    The below code snippet will give you the meta data of an audio file. You need to add an audio file in Resource Folder. Once that is done use the below code. Let us also understand the code.

     

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        NSString *audioFilePath = [[NSBundle mainBundle]pathForResource:@"Valentine" ofType:@"mp3"];	// 1
        NSURL *audioURL = [NSURL fileURLWithPath:audioFilePath];	// 2
        NSLog (@"audio File URL: %@", audioURL);
        
        AudioFileID audioFile;	// 3
        OSStatus theErr = noErr;	// 4
        theErr = AudioFileOpenURL((__bridge CFURLRef)audioURL, kAudioFileReadPermission, 0, &audioFile); // 5
        assert (theErr == noErr);	// 6
        
        UInt32 dictionarySize = 0;	// 7
        theErr = AudioFileGetPropertyInfo (audioFile, kAudioFilePropertyInfoDictionary,
                                           &dictionarySize, 0); // 8
        assert (theErr == noErr);	// 9
        
        CFDictionaryRef dictionary;	// 10
        theErr = AudioFileGetProperty (audioFile, kAudioFilePropertyInfoDictionary,
                                       &dictionarySize, &dictionary); // 11
        assert (theErr == noErr);	// 12
        
        NSLog (@"Song Info Data: %@", dictionary);	// 13
        CFRelease (dictionary);	// 14
        theErr = AudioFileClose (audioFile);	// 15
        assert (theErr == noErr);	// 16
    }
    

    1. A path to an audio file added in Resource Folder in project workspace.

    2. Convert the string to NSURL as Core Audio API's work with NSURL.

    3. Audio file objects are referred by AudioFileID type, so you must declare a reference as a local variable.

    4. Almost all Core Audio functions return success or failure as their signal value, the type of that signal is OSStatus. Any status other than noErr (i.e 0) signals an error. You must check this signal value on every Core Audio api call, because it makes no sense to check the last API call, and leaving all other checks. Since there can be error at any API call. So subsequent API calls have no meaning in it.

    5. This is the first Core Audio function call: AudioFileOpenURL. There are four parameters in this function, A CFURLRef, a permission flag, a file type hint, and a pointer to receive the created AudioFileID Object. Type cast the NSURL to CFURLRef by using __bridge keyword to match the first parameter. Pass a file read permission constant to read the file. Pass 0 for the hint as Core Audio will figure it out. For the last parameter, pass the & "address of" operator to provide a pointer to receive the AudioFileID object.

    6. If AudioFileOpenURL returns an error, the code will exit and will display the error.

    7.  To get the file metadata, we need to use the property kAudioFilePropertyInfoDictionary. But that API call requires allocating memory first. So, we need to declare a local variable to receive the size we'll need to allocate.

    8. To get the size, we need to call AudioFileGetPropertyInfo function, it takes four parameters, an AudioFileID object, the property about the information needed, a pointer to receive the size, and a pointer to a flag variable indicating whether the property is writable (as we don't care, pass it 0).

    9. If AudioFileGetPropertyInfo fails, program terminates.

    10. To get the meta data information we need a variable of type CFDictionaryRef,  as kAudioFilePropertyInfoDictionary results in a dictionary.

    11. Call the function AudioFileGetProperty to get the meta data information. There are 4 parameters to be passed in the function, an AudioFileID object, the property constant, a pointer to the size of the data that is about to receive, and a pointer to receive the value.

    12. Again, check the return value and fail if it’s anything other than noErr.

    13. Get the string representation of a dictionary by passing "%@" as the format specifier for string.

    14. Release the Core foundation object by using CFRelease function.

    15. The AudioFileID also needs to be cleaned up. It is not a Core Foundation object so we cannot use CFRelease() function for that. To clean up AudioFileID object we need to use AudioFileClose() function.

    16. AudioFileClose() is another API call, so continue to check the return code.

    So, thats the code to get the meta data of an audio file. Opening a File, Allocating a buffer for metadata, and getting the metadata.

    You can download the code.

    Thanks for reading. Happy Coding :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: