1

This code worked, but when I moved to iOS 6 it stopped supporting mp4.

I prepare write

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"Temp.mp4"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
                                [NSNumber numberWithFloat: 32000], AVSampleRateKey,
                                [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
                                [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                                nil];
NSError *error = nil;
self.audioRecorder = nil;
AVAudioRecorder *audioRecorderBuffer = [[AVAudioRecorder alloc]
                                        initWithURL:soundFileURL
                                        settings:recordSettings
                                        error:&error];
self.audioRecorder = audioRecorderBuffer;
self.audioRecorder.delegate = self;
if (error)
    NSLog(@"error: %@", [error localizedDescription]);
else
    [self.audioRecorder prepareToRecord];

then record, sto, save it and when I try to play:

_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:_audioURL error:&error];
_audioPlayer.delegate = self;
if (error)
    NSLog(@"Error: %@", [error localizedDescription]);
else {
    [_audioPlayer prepareToPlay];
    if (![_audioPlayer play]) {
        NSLog(@"Play Error: %@", error);
    }
}

I get and error:

Error: The operation couldn’t be completed. (OSStatus error 1685348671.)

The code works on uncompressed format. Compressed mp4 works on iPhone 4 with iOS 6, worked also on iphone 4S with iOS 5.x, but does not work on iphone 4S nor iphone 5 with iOS 6.

AVAudioRecorder records and writes the file, then AVAudioPlayer has error Error: The operation couldn’t be completed. (OSStatus error 1685348671.). Documentation states that file format is corrupted.

Any ideas?

janpawelw
  • 11
  • 3
  • I have exactly the same issue. It finish the recording, but files are not playable. Any comment? – Shahab Sep 04 '13 at 09:34

2 Answers2

0

Try adding the following before [self.audioRecorder prepareToRecord]

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];
[session setActive:YES error:nil];

Check out this question/answer

Community
  • 1
  • 1
Chris
  • 95
  • 1
  • 1
  • 8
0

I've just had a very similar issue with corrupted files - for me it came down to the fact that those who couldn't record had denied permission to access the microphone. This meant that prepareToRecord was working (and truncating the file) and record was reporting that it was working, but actually it wasn't recording anything.

This code (swift not Objective-C) gets the permission state:

let val : AVAudioSessionRecordPermission = AVAudioSession.sharedInstance().recordPermission()
switch( val ){
case AVAudioSessionRecordPermission.Undetermined :
    print("permission undetermined")
    break
case AVAudioSessionRecordPermission.Denied :
    print("permission denied")
    break
case AVAudioSessionRecordPermission.Granted :
    print("permission granted")
    break
default :
    print("permission unknown")
    break
}

And I used this in when I want to record:

AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool)-> Void in
    if granted {
        self.internalRecorder.prepareToRecord()
        self.internalRecorder.record()
    } else {
        // can't record - update UI
    }
})

Hope it saves someone some time if they run into the same issue.

atomoil
  • 364
  • 1
  • 7