AVAudioRecorder失去最后一秒 [英] AVAudioRecorder Losing Last Seconds
问题描述
我正在使用AVAudioRecorder来录制具有以下配置的不同长度的音频文件:
I'm using AVAudioRecorder to record an audio file of varying length with the following configuration:
- (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秒(而且绝对是必需的.)
When I call [self.recorder stop]
the last 2-3 seconds of the recording are never captured (and they are definitely needed.)
例如,如果我在调用[self.recorder stop]
之前立即调用self.recorder.currentTime
,我将得到一个NSTimeInterval,该值比生成的文件的实际持续时间长大约2-3秒.然后,在2-3秒内记录的所有内容都会丢失.
For example, if I call self.recorder.currentTime
right before I call [self.recorder stop]
I will get a NSTimeInterval greater than the actual duration of the generated file by about 2-3 seconds. Anything recorded in that 2-3 seconds is then lost.
在调用[self.recorder stop]
之后,我将调用相应的audioRecorderDidFinishRecording:successfully:
委托方法,并且该标志指示该方法成功.
After calling [self.recorder stop]
I get the appropriate audioRecorderDidFinishRecording:successfully:
delegate method called and the flag indicates it is successful.
我对这些最后几秒钟的丢失情况一无所知. (记录器没有被释放,仍然有很强的参考意义.)
I'm at a total loss as to how these last seconds are being lost. (The recorder is not being released and there is still a strong reference to it.)
更新
该应用程序会提示用户多次讲话,因此AVAudioRecorder会暂停并恢复多次,然后在最终响应结束时停止.它被暂停而不是停止,因为所有这些都需要记录到一个音频文件中.
The app prompts the user to speak multiple times, so the AVAudioRecorder is paused and resumed multiple times before being stopped at the end of the final response. It is paused instead of stopped because it all needs to be recorded to a single audio file.
推荐答案
通过查看您共享的有限代码,我只能怀疑您可能不正确地使用了记录器对象.
By looking at limited code you shared, I can only suspect that you might be using the recorder object improperly.
下面的要点和可能会帮助您的后续代码.(对我有用)
Key points below and follow up code that might help you.. (it works for me)
1)每次开始新录制时,都应创建/初始化AVAudioRecorder.
1) You should create/init AVAudioRecorder every time you begin a new recording.
2)AVAudioRecorder的currentTime属性仅在进行录制时有效(即isRecording为YES)
2) currentTime property of AVAudioRecorder is only valid when recording is in progress (i.e. isRecording is 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
委托方法中录制呼叫stop
后,请使用以下代码(基于URL)检查音频持续时间吗?只是交叉验证currentTime
不正确.
After your call stop
recording using [self.audioRecorder stop]
or inside audioRecorderDidFinishRecording
delegate method, please check audio duration using the below code (url based)? Just to cross verify that the currentTime
is not correct.
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);
这篇关于AVAudioRecorder失去最后一秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!