iphone:AVAudioRecorder录音总是返回FALSE

发布于 2024-12-12 05:01:33 字数 950 浏览 0 评论 0原文

我正在使用 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];

I am using AVAudioRecorder to record sound, but I have a problem.

After calling the method [recorder record], the value of recorder.recording always returns FALSE, so that I can not stop the recording.

I am using XCode 4.2 with iOS 5, my iPad is ipad 2 on iOS 5 also.

This is my code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

昇り龍 2024-12-19 05:01:33

尝试在 [audioRecordprepareToRecord] 之后使用 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];

try using AVAudioSession after [audioRecord prepareToRecord]

    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];
银河中√捞星星 2024-12-19 05:01:33

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

因此,在开始录制之前激活您的会话

For 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

An audio session is the intermediary between your app and iOS used to configure your app’s audio behavior. Upon launch, your app automatically gets a singleton audio session.

so activate your session before start recording

For 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
迷路的信 2024-12-19 05:01:33

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

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

我正在使用的库将其重置为 AVAudioSessionCategoryPlay。如果除了 false 之外还有其他访问错误,让您知道导致 record() 失败的原因,那就太好了。

I had a similar symptom, because I was using a library to playback that was resetting AVAudioSession global settings. I needed to reset the settings each time I wanted to record.

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

The library I'm using resets it to AVAudioSessionCategoryPlay. Sure would be nice if there was an error to access other than false to let you know what caused the failure of record().

探春 2024-12-19 05:01:33

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

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

Are you also seeing a delay when you invoke the record method? I'm seeing an issue when using iOS5 on iPhone4 hardware where the record method has a 3-4 second delay and occasionally will fail to start recording. I'm calling prepareToRecord as recommended with the same issue.

This is not an issue on iOS4/iPhone4 or iOS5/iPhone4S...so it seems to be an issue running the new OS on older hardware.

终止放荡 2024-12-19 05:01:33

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

I encountered the same problem on Simulator, and after investigation I found that it was because my computer didn't set up any audio input sources. Once I connected an input source (e.g., AirPod) and record() finally returned true and started to work. Sadly, record() never gives any error to indicate this type of failure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文