Question

I have an app that uses AVAudioRecorder and AVAudioPlayer. It records and plays fine but sometimes recordings get cut off right around the one minute mark. This doesn't happen all of the time and when it does, it still acts like it's recording (audioRecorderEncodeErrorDidOccur doesn't get called and audioRecorderDidFinishRecording does when the user stops the recording). The sound file itself, though is truncated at under a minute.

Here is the encoding I'm using (soundFileURL is appsdocdir/somethingrandom.m4a The file and filename get created properly):

NSDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];

NSError *error = nil;
self->audioRecorder = nil;
audioRecorder = [[AVAudioRecorder alloc]
                 initWithURL:soundFileURL
                 settings:recordSetting
                 error:&error];

if (error)
{
    NSLog(@"error: %@", [error localizedDescription]);

} else {
    audioRecorder.delegate = self;
    [audioRecorder prepareToRecord];
    audioRecorder.meteringEnabled = YES;
}

I think its an encoding issue because if I look at the device log I see this:

May 10 10:17:23 t-iPhone mediaserverd[39] : 10:17:23.451 <0x282f000> AudioConverterNew returned -50

May 10 10:17:23 t-iPhone mediaserverd[39] : 10:17:23.453 <0x282f000> IO_ChangeIOFormat: error -50

May 10 10:17:23 t-iPhone mediaserverd[39] : 10:17:23.463 <0x282f000> Queue_ChangeIOFormat: failed (-50)

EDIT: It was asked what soundFileURL was. I briefly mentioned it above, but here is the actual code. The file and filenames all look correct after recording:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMMdyyyyA"];
NSString *recordDate = [formatter stringFromDate:[NSDate date]];

filename = [[NSString alloc] initWithFormat:@"%d-%@.m4a", idNumber, recordDate];
NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(
                                               NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
                           stringByAppendingPathComponent:filename];
soundFileURL = [NSURL fileURLWithPath:soundFilePath];
Was it helpful?

Solution

I figured it out. The problem was the phone going to sleep. It would cut out any audio from when it was asleep to when it came out of the lock screen. I fixed by adding UIBackgroundModes -> audio to the info.plist file:

http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top