AVAudioRecorder Record方法随机返回NO [英] AVAudioRecorder Record method returns NO at random times

查看:650
本文介绍了AVAudioRecorder Record方法随机返回NO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一款支持应用内录音的应用。用户可以获得他/她已经通过应用程序记录的本地保存的音频文件的标准表格视图,或者按下按钮转到新的录制视图,从那里可以录制新的音频会话,这将自动获得保存到应用程序沙箱。
现在这个功能大部分时间都运行良好。我可以录制新文件并从应用程序播放它们,但随机时间录音机将无法以此方法开始录制:

I’m currently developing an app which supports in-app audio recording. The user can get a standard tableview of locally saved audio files he/she have already recorded through the app, or press a button to go to a new recording view, from where it is possible to record a new audio session, which will automatically get saved to the apps sandbox. Now this function works well most of the time. I can record new files and play them from the app, but at random times the audio recorder will fail to start a recording in this method:

-(void) startRecording
{
if (!isRecording)
{
    isRecording = YES;

    BOOL recordingSucess = [audioRecorder record];

    if (recordingSucess)
        NSLog(@"WE STARTED THE RECORDING!");
    else
        NSLog(@"WE DID NOT START THE RECORDING!");

    // Start the timer
    recordTiming = [NSDate date];

    timeCheck = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timeCheck) userInfo:nil repeats:YES]
}

也就是说,NSLog将打印出我们没有开始录制,表示它无法开始录制。这导致仍然将空音频文件保存到应用程序,文件大小通常为4096字节。
这是录音机对象的初始化方式:

That is, the NSLog will print out "We did not start the recording", indicating that it failed to start the recording. This results in an empty audio file still being saved to the app with a constant file size of exactly 4096 bytes. This is how the audio recorder object is initialized:

-(id) initWithContentURL:(NSURL *)contentURL
{
self = [super init];

if (self)
{
    NSLog(@"Initializing audio recorder!");

    // Specific settings for the audio recording
    NSDictionary *recordSettings = [NSDictionary 
                                    dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                    AVEncoderAudioQualityKey,
                                    [NSNumber numberWithInt:8], 
                                    AVEncoderBitRateKey,
                                    [NSNumber numberWithInt: 2], 
                                    AVNumberOfChannelsKey,
                                    [NSNumber numberWithFloat:22000.0], 
                                    AVSampleRateKey,
                                    nil];

    NSError *error = nil;

    audioRecorder = [[AVAudioRecorder alloc] initWithURL:contentURL settings:recordSettings error:&error];
    audioRecorder.delegate = self;
}

还有NSLog检查,检查错误是否为零,所以在这一点上我确信初始化似乎没有失败。
究竟什么原因可能导致记录方法在看似随机的时间内失败,而在大多数其他时间它的效果非常好?

There is also NSLog checks which checks to see if the error is nil or not, so at this point I’m sure that the initialization does not seem to fail. What exactly could cause the record method to fail at seemingly random times, while it works perfectly good at most other times?

推荐答案

在录制开始之前,应初始化音频会话:

Before the recording is started an audio session should be initialized:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
if(err){
   NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
   return;
}
err = nil;
[audioSession setActive:YES error:&err];
if(err){
   NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
   return;
}

查看已接受的回答供参考。

这篇关于AVAudioRecorder Record方法随机返回NO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆