Challenge, recording works on simulator, not on device


I’m not sure why this isn’t working. I create an AVAudioRecorder for a file in the documents directory. There are no errors.
When I later call [audioRecorder record], the return value is NO on the iDevice (iPod touch 4G, 5.0.1), but on the iPhone simulator 5.0 it works (and playback works also). I have tried recording to a file in NSTemporaryDirectory() instead of docDir… and also various permutations of settings and formats and file endings. Some combinations work on the simulator and some don’t --some even record but play back incorrectly-- but I haven’t gotten anything to work on the device yet.

Does anyone have a suggestion or see something wrong?
Could someone post some working (on a device) initialization code for an AVAudioRecorder?
Is there a list anywhere of which settings for AVAudioRecorder will work with which devices/iOS-version?


        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask, YES);
        NSString *docDir = [paths objectAtIndex:0];
        NSString *recordPath = [docDir stringByAppendingString:@"/recording.caf"];
        NSURL *recordURL = [NSURL fileURLWithPath:recordPath];
        NSDictionary *settingsDict =
        [NSDictionary dictionaryWithObjectsAndKeys:
            [NSNumber numberWithInt:kAudioFormatLinearPCM],
            [NSNumber numberWithFloat:16000.0],
            [NSNumber numberWithInt:1],
            [NSNumber numberWithInt:16],
            [NSNumber numberWithBool:NO],
            [NSNumber numberWithBool:NO],
        NSError *error;
        audioRecorder = [[AVAudioRecorder alloc] initWithURL:recordURL


Ok, I’ve found the problem(s).

First, from the first part of the tutorial, I was calling:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlay error:nil];

For recording sound also, one needs (but in the simulator it doesn’t matter…):

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

Second, apparently when you create the AVAudioPlayer, it reads in the audio format from the file at that time, not when you call play. So the AVAudioPlayer needs to be created when the button is pressed.

Neither of these problems were evident in the code snippet in my original post. Sorry. Since the simulator was working I had assumed I was using a format not supported on the device but supported in mac os.

The settings I have above should work for a recorder. Changing endianness works, 1 or 2 channels work, sample rates 8000, 22050, 44100 work.

Here is a page I found showing what compression formats should work (note that aac has some hardware acceleration)

I still haven’t found any information about allowable number of channels or sample rate.

Here is a page I found showing what file types should work for playback (careful! this is for Mac OS X despite the ios logo).
There is some information about bits per sample and endianness for the various formats. It looks like you’re safe using a .caf file for any allowable encoding that isn’t exported from the device.