繁体   English   中英

iphone:AVAudioRecorder 录音总是返回 FALSE

[英]iphone: AVAudioRecorder recording always return FALSE

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

调用方法[recorder record]后,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];

在 [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];

音频会话是您的应用和 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

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

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

我正在使用的库将其重置为AVAudioSessionCategoryPlay 如果有错误访问而不是false让您知道导致record()失败的原因,那当然会很好。

我在模拟器上遇到了同样的问题,经过调查发现是因为我的电脑没有设置任何音频输入源。 一旦我连接了输入源(例如,AirPod), record()最终返回true并开始工作。 可悲的是, record()从来没有给出任何错误来指示这种类型的失败。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM