0

我正在使用 AVAudioRecorder 录制具有以下配置的不同长度的音频文件:

- (AVAudioRecorder*)recorder
{
    if(!_recorder) {
        // Set the audio file
        NSArray *pathComponents = [NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], @"ExamTranscript.m4a", nil];
        self.soundFileURL = [NSURL fileURLWithPathComponents:pathComponents];

        // Define the recorder setting
        NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

        [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
        [recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];
        [recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];

        // Initiate and prepare the recorder
        _recorder = [[AVAudioRecorder alloc] initWithURL:self.soundFileURL settings:recordSetting error:NULL];
        _recorder.meteringEnabled = YES;
        _recorder.delegate = self;
    }

    return _recorder;
}

当我打电话[self.recorder stop]时,录音的最后 2-3 秒永远不会被捕获(而且它们肯定是需要的。)

例如,如果我在打电话self.recorder.currentTime之前打电话[self.recorder stop],我会得到一个比生成文件的实际持续时间大 2-3 秒的 NSTimeInterval。在那 2-3 秒内记录的任何内容都会丢失。

调用后,[self.recorder stop]我调用了适当的audioRecorderDidFinishRecording:successfully:委托方法,并且标志表明它是成功的。

我完全不知道这些最后几秒钟是如何丢失的。(录音机没有发布,仍然有强烈的参考。)

更新

该应用程序会多次提示用户说话,因此 AVAudioRecorder 会多次暂停和恢复,然后在最终响应结束时停止。它被暂停而不是停止,因为它都需要录制到单个音频文件中。

4

1 回答 1

1

通过查看您共享的有限代码,我只能怀疑您可能不正确地使用了记录器对象。

下面的关键点和可能对您有帮助的后续代码..(它适用于我)

1)您应该在每次开始新录音时创建/初始化 AVAudioRecorder。

2) AVAudioRecorder 的 currentTime 属性仅在录制过程中有效(即 isRecording 为 YES)

// class properties
@property (nonatomic, retain) AVAudioRecorder *audioRecorder;
@property (nonatomic, retain) NSDictionary *recordQualitySettings;
@property (nonatomic, assign) NSInteger maxRecordDurationInSeconds;

// class's designated initializer
- (instancetype)init
{
    self = [super init];

    if (self) {
        self.recordQualitySettings = [NSDictionary
                               dictionaryWithObjectsAndKeys:
                               [NSNumber numberWithInt:kAudioFormatAppleIMA4],
                               AVFormatIDKey,
                               [NSNumber numberWithInt:1],
                               AVNumberOfChannelsKey,
                               [NSNumber numberWithFloat:16000.0],  //??
                               AVSampleRateKey,
                               nil];
...
...
    }

    return self;
}

- (void)recordBegin
{
    NSString *temnpVoiceFile = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], @"tempVoice.caf", nil]];
    NSURL *soundFileURL = [NSURL fileURLWithPath:temnpVoiceFile];
    NSError *error = nil;

    // Instantiate an audio recorder.
    self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:self.recordQualitySettings error:&error];
    NSAssert1(!error, @"error creating audio file = %@", error);

    self.audioRecorder.delegate = self;
    self.audioRecorder.meteringEnabled = YES;

    [self.audioRecorder recordForDuration:self.maxRecordDurationInSeconds];   // Any of the 'record..' methods that triggers recording.

}

编辑:

使用或内部委托方法进行通话stop录音后,请使用以下代码(基于 URL)检查音频持续时间?只是为了交叉验证不正确。[self.audioRecorder stop]audioRecorderDidFinishRecordingcurrentTime

AVURLAsset* audioAsset = [AVURLAsset URLAssetWithURL:self.audioRecorder.url options:nil];   // delegate method provide you `recorder` object as parameter too.
CMTime audioDuration = audioAsset.duration;
double duration = CMTimeGetSeconds(audioDuration);
于 2013-12-16T21:52:51.520 回答