One Quickie


Getting the duration of an audio file (Audio->General)
#import <AudioToolbox/AudioToolbox.h>
...
- (NSTimeInterval) voiceCueLength {
    NSTimeInterval length = 0.0;
    NSURL *audioURL = [NSURL fileURLWithPath: self.pathToVoiceCue];

    OSStatus result = noErr;

    AudioFileID audioFile = NULL;
    result = AudioFileOpenURL ((CFURLRef)audioURL,
                               kAudioFileReadPermission,
                               0, // hint
                               &audioFile);
    if (result != noErr) goto bailout;

    //Get file length
    NSTimeInterval seconds;
    UInt32 propertySize = sizeof (seconds);
    result = AudioFileGetProperty (audioFile,
                                   kAudioFilePropertyEstimatedDuration,
                                   &propertySize,
                                   &seconds);
    if (result != noErr) goto bailout;

    length = seconds;

  bailout:
    if (audioFile) AudioFileClose (audioFile);
    return length;
} // voiceCueLength
And Link with the AudioToolbox framework.



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com