简体   繁体   English

AVAudioRecorder Record方法在随机时间返回NO

[英]AVAudioRecorder Record method returns NO at random times

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

That is, the NSLog will print out “We did not start the recording”, indicating that it failed to start the recording. 也就是说,NSLog将打印出“我们没有开始录制”,表示它无法开始录制。 This results in an empty audio file still being saved to the app with a constant file size of exactly 4096 bytes. 这导致仍然将空音频文件保存到应用程序,文件大小通常为4096字节。 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;
}

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. 还有NSLog检查,它检查错误是否为零,所以此时我确信初始化似乎没有失败。 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;
}

See the accepted answer for reference. 请参阅接受的答案以供参考。

Is contentURL always the same or do you generate a new one each time? contentURL是否始终相同,或者每次都生成一个新的?

Also could you try explicitly calling prepareToRecord before record and checking whether it succeeds or not? 您也可以尝试在记录之前显式调用prepareToRecord并检查它是否成功?

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

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