我正在使用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]
,我将得到一个NSTimeInterval,它比生成的文件的实际持续时间长大约2-3秒。在那2-3秒内记录的所有内容都会丢失。调用
[self.recorder stop]
之后,我将调用相应的audioRecorderDidFinishRecording:successfully:
委托方法,并且该标志指示成功。关于这最后几秒的丢失,我完全不知所措。 (记录器没有被释放,仍然有很强的参考意义。)
更新
该应用程序提示用户多次讲话,因此AVAudioRecorder会暂停并恢复多次,然后在最终响应结束时停止。它被暂停而不是停止,因为所有这些都需要记录到一个音频文件中。
最佳答案
通过查看您共享的有限代码,我只能怀疑您可能未正确使用记录器对象。
下面的要点和可能会帮助您的后续代码(对我有用)
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
或在[self.audioRecorder stop]
委托方法中呼叫audioRecorderDidFinishRecording
录制后,请使用以下代码(基于URL)检查音频持续时间吗?只是交叉验证currentTime
不正确。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);
关于ios - AVAudioRecorder失去最后一秒,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20620841/