views:

392

answers:

1

I am trying to record using the iphones microphone: This is my code:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"testing.mp3"];




NSURL *url = [NSURL fileURLWithPath:appFile isDirectory:NO];

NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
                          [NSNumber numberWithInt: kAudioFormatMPEGLayer3], AVFormatIDKey,
                          [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                          [NSNumber numberWithInt: AVAudioQualityLow],         AVEncoderAudioQualityKey,
                          nil];

NSError *error;

recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];

if ([recorder prepareToRecord] == YES){
    [recorder record];
}else {
    int errorCode = CFSwapInt32HostToBig ([error code]); 
    NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode); 

}

NSLog(@"BOOL = %d", (int)recorder.recording);

This is the error I get:

Operation could not be completed. (OSStatus error 1718449215.)

And I can not work out why this doesn't work, as a lot of the code I got from a website.

Jonathan

+2  A: 

Your problem is kAudioFormatMPEGLayer3

Unfortunately you can't encode sound on iPhone in mp3 format. At present time best format for compressed audio is kAudioFormatAppleIMA4. (yes, there is also kAudioFormatMPEG4AAC - even better, but it works only on simulator, but not on device). Note also that there can be problems with decoding of recorded kAudioFormatAppleIMA4 file on other systmes e.g. windows, so simple lpcm format may be more convenient for small files.

Vladimir
Thanks, This finally worked :)Do you know how to record to memory, so that i can play the recording back, with like a 3 second delay?
Jonathan
Not with AVAudioRecorder. You should use audio queue service for this purpose. See http://developer.apple.com/iphone/library/documentation/MusicAudio/Conceptual/AudioQueueProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40005343-CH1-SW1 and speak here example (http://developer.apple.com/iphone/library/samplecode/SpeakHere/)
Vladimir