After I updated app for iOS 6, AVAudioRecorder
doesn't work on a device, and crashing in Simulator on [soundRecorder prepareToRecord]
.
Upd.: audioRecorderDidFinishRecording:successfully:
delegate method fires with no delay after [soundRecorder record];
Does anyone find some fix?
- (IBAction)recordSound {
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate.audioPlayer stop];
appDelegate.audioPlayer = nil;
[self stopPlaying];
if (soundRecorder.recording) {
[soundRecorder stop];
soundRecorder = nil;
[timer invalidate];
timer = nil;
NSError *error;
[[AVAudioSession sharedInstance] setActive: NO error: &error];
NSLog([error localizedDescription]);
}else{
NSManagedObject *oldSound = _sight.sound;
if (oldSound != nil) {
[__managedObjectContext deleteObject:oldSound];
}
[self saveContext];
if (!soundRecorder)
{
NSError *errorAudioSession;
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryPlayAndRecord
error: &errorAudioSession];
NSLog([errorAudioSession description]);
NSDictionary *recordSettings =
@{AVFormatIDKey: @(kAudioFormatMPEG4AAC),
AVNumberOfChannelsKey: @1,
AVEncoderAudioQualityKey: @(AVAudioQualityMedium)};
NSError *error;
AVAudioRecorder *newRecorder =
[[AVAudioRecorder alloc] initWithURL: soundFileURL
settings: recordSettings
error: &error];
NSLog([error description]);
soundRecorder = newRecorder;
soundRecorder.delegate = self;
}
[soundRecorder stop];
[soundRecorder prepareToRecord];
[soundRecorder record];
timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateRecordStatus) userInfo:nil repeats:YES];
}
}
I had a problem with audio recording ending as soon as I told it to start recording after the first time in iOS6 (in iOS5 worked great). The issue was that I was setting the AVAudioRecorder's delegate but in iOS6 AVAudioSession's delegate is deprecated, removing the delegate made my app work again.
EDIT: as @Flink asked. I have a wrapper class for recording audio (only extends NSObject) that uses the AVAudioRecorder. The relevant part of the code is below:
...
NSError *error = nil;
audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];
//the relevant part of code is this "if"
if(SYSTEM_VERSION_LESS_THAN(@"6") && delegate){
audioRecorder.delegate = self.delegate;
}
BOOL ok = [audioRecorder prepareToRecord];
if (ok){
if(duration){
ok = [audioRecorder recordForDuration:duration];
}
else {
ok = [audioRecorder record];
}
if(!ok) {
LogError(...);
}else {
LogInfo(@"recording");
}
}else {
LogError(...);
}
...