我正在使用 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 stop]
之前调用 self.recorder.currentTime
,我将得到一个大于实际持续时间的 NSTimeInterval生成文件大约需要 2-3 秒。在那 2-3 秒内记录的任何内容都会丢失。
调用 [self.recorder stop]
后,我得到了适当的 audioRecorderDidFinishRecording:successfully:
调用委托(delegate)方法,标志指示它成功。
我完全不知道这些最后几秒是如何丢失的。 (记录器没有发布,仍然有强烈的引用。)
更新
应用程序多次提示用户说话,因此 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.
}
编辑:
在使用 [self.audioRecorder stop]
或在 audioRecorderDidFinishRecording
委托(delegate)方法中调用 stop
录音后,请使用以下代码检查音频持续时间(基于网址)?只是为了交叉验证 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/