在iOS6中更新后,AVAudioRecorder不起作用

时间:2012-10-31 17:56:20

标签: objective-c ios6 avaudiorecorder

更新iOS 6的应用后,AVAudioRecorder无法在设备上运行,并在[soundRecorder prepareToRecord]上的模拟器中崩溃。

更新:audioRecorderDidFinishRecording:successfully:委托方法在[soundRecorder record];

之后毫不拖延地触发

有没有人找到一些修复方法?

- (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];

    }
}

1 个答案:

答案 0 :(得分:0)

一旦我告诉它在iOS6中第一次开始录制(iOS5工作得很好),我的录音结束就出现了问题。 问题是我正在设置AVAudioRecorder的委托,但是在iOS6中,不推荐使用AVAudioSession的委托,删除委托使我的应用程序再次运行。

http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioSession_ClassReference/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instp/AVAudioSession/delegate

@Flink问道,

编辑:。我有一个包装类用于录制使用AVAudioRecorder的音频(仅扩展NSObject)。代码的相关部分如下:

...
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(...);    
}
...