CaptureSessionController.mm in AVCaptureToAudioUnit won't record wav, aac or mp3 files

2

I'm using sample code of AVCaptureToAudioUnit to record voice using iPhone as a microphone. I used this example as a starting point because I had more success with this sample project than with other sample projects.

When the file is .aif or .caf the demo app runs fine but when I create a .wav, .aac or .mp3 file, the following message appears in the debug console

AudioStreamBasicDescription: 1 ch, 44100 Hz, 'lpcm' (0x0000000E) 16-bit big-endian signed integer

2013-07-26 19:52:06.653 AVCaptureToAudioUnit[2514:907] Failed to setup audio file! (29759)

To change file format I made two changes

[a] the file extension in the NSString statement (see change in init below) and

[b] compatible settings using constants defined in the Audio File Services Reference (in startRecording).

Do I have to change other properties when using other file formats ? Has anyone experienced this problem ?

Here is the code for [a]

@implementation CaptureSessionController

#pragma mark ======== Setup and teardown methods =========

- (id)init
{
    self = [super init];

    if (self) {

    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    //NSString *destinationFilePath = [NSString stringWithFormat: @"%@/AudioRecording.aac", documentsDirectory];
    //NSString *destinationFilePath = [NSString stringWithFormat: @"%@/AudioRecording.caf", documentsDirectory];
    //NSString *destinationFilePath = [NSString stringWithFormat: @"%@/AudioRecording.wav", documentsDirectory];
    //NSString *destinationFilePath = [NSString stringWithFormat: @"%@/AudioRecording.mp3", documentsDirectory];

    // and the following statement is the line of code found in the original example

    NSString *destinationFilePath = [NSString stringWithFormat: @"%@/AudioRecording.aif", documentsDirectory];
    _outputFile = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)destinationFilePath, kCFURLPOSIXPathStyle, false);

    [self registerForNotifications];
    }

return self;

}

And here is the code for [b]

- (void)startRecording
{
    if (!self.isRecording) {
    OSErr err = kAudioFileUnspecifiedError;
    @synchronized(self) {
    if (!extAudioFile) {
/*
    Start recording by creating an ExtAudioFile and configuring it with the same 
    sample rate and channel layout as those of the current sample buffer.
*/
    // recording format is the format of the audio file itself
    CAStreamBasicDescription recordingFormat(currentInputASBD.mSampleRate, currentInputASBD.mChannelsPerFrame, CAStreamBasicDescription::kPCMFormatInt16, true);
    recordingFormat.mFormatFlags |= kAudioFormatFlagIsBigEndian;

    NSLog(@"Recording Audio Format:");
    recordingFormat.Print();

    err = ExtAudioFileCreateWithURL(_outputFile,

    //kAudioFileAAC_ADTSType,   // won't restart recording "Failed to setup audio file"
    //kAudioFileCAFType,        // starts and stops correctly
    //kAudioFileWAVEType,       // won't restart recording "Failed to setup audio file"
    //kAudioFileMP3Type,        // won't restart recording "Failed to setup audio file"
    kAudioFileAIFFType,       // starts and stops correctly

    &recordingFormat,
    currentRecordingChannelLayout,
    kAudioFileFlags_EraseFile,
    &extAudioFile);

    if (noErr == err)

    // client format is the output format from the delay unit
    err = ExtAudioFileSetProperty(extAudioFile, kExtAudioFileProperty_ClientDataFormat, sizeof(graphOutputASBD), &graphOutputASBD);

    if (noErr != err) {

                        if (extAudioFile) ExtAudioFileDispose(extAudioFile);
                        extAudioFile = NULL;
                      }
                  }
              } // @synchronized

    if (noErr == err) {
        self.recording = YES;
      NSLog(@"Recording Started");
       } else {
        NSLog(@"Failed to setup audio file! (%ld)", (long)err);
         }
    }
}
mp3
wav
aac
caf
asked on Stack Overflow Jul 27, 2013 by Greg • edited Feb 21, 2015 by FH-Inway

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0