iphone:AVAudioRecorder录音总是返回FALSE

时间:2011-10-27 10:36:12

标签: iphone ios ipad ios5 avaudiorecorder

我正在使用AVAudioRecorder录制声音,但我遇到了问题。

调用[记录器记录]方法后,recorder.recording的值总是返回FALSE,这样我就无法停止录制。

我在iOS 5上使用XCode 4.2,我的iPad在iOS 5上也是ipad 2。

这是我的代码:

    self.currentFileName = [GlobalRecording newRecordingFileName];
    NSURL *url = [NSURL fileURLWithPath:[[GlobalRecording recordingDirectory]    stringByAppendingPathComponent:currentFileName]];
NSError *err = nil;

AVAudioRecorder *aRecorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordingSettings error:&err];
if( err ){
    NSLog(@"could not create a recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    return;
}

self.startRecordingTime = [NSDate date];
recordingLength = 0.0f;

//  setup recorder and start recording
[aRecorder setDelegate:self];
aRecorder.meteringEnabled = YES;
[aRecorder record];

self.recorder = aRecorder;
BOOL test = recorder.recording;
[aRecorder release];

4 个答案:

答案 0 :(得分:3)

在[audioRecord prepareToRecord]

之后尝试使用AVAudioSession
    NSError *err = nil;
audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];

// route to speaker
UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(ASRoute), &ASRoute);

if (err)
{
    NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
}

[audioSession setActive:YES error:&err];

答案 1 :(得分:1)

音频会话是您的应用和iOS之间的中介,用于配置应用的音频行为。启动后,您的应用会自动获得单个音频会话。

所以在开始录制之前激活你的会话

对于Swift 3.0

let session = AVAudioSession.sharedInstance()
        do {
            try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
        } catch let error as NSError {
            print("could not set session category")
            print(error.localizedDescription)
        }
        do {
            try session.setActive(true)
        } catch let error as NSError
        {
            print("could not make session active")
            print(error.localizedDescription)
        }
self.recorder.record() // recorder is my AVAudioRecorder

答案 2 :(得分:0)

您在调用记录方法时是否也看到了延迟?我在iPhone4硬件上使用iOS5时遇到问题,其中记录方法有3-4秒的延迟,偶尔会无法开始录制。我正在按照同样的问题推荐prepareToRecord。

这不是iOS4 / iPhone4或iOS5 / iPhone4S上的问题......所以在旧硬件上运行新操作系统似乎是一个问题。

答案 3 :(得分:0)

我有一个类似的症状,因为我正在使用一个库来重设AVAudioSession全局设置。每次想要录制时,我都需要重置设置。

do {
  try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
  try session.setActive(true)
} catch {
  return error
}

我正在使用的库将其重置为AVAudioSessionCategoryPlay。如果没有访问false以外的其他错误来通知您record()失败的原因,肯定会很好。