0

在我为 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];

    }
}
4

1 回答 1

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(...);    
}
...
于 2012-12-13T15:46:53.847 回答