我用AVAudioRecorder記錄與以下配置不同長度的音頻文件: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
,我將得到一個大於生成文件實際持續時間大約2-3秒的NSTimeInterval。然後在2-3秒內記錄的任何內容都將丟失。
在致電[self.recorder stop]
後,我得到相應的audioRecorderDidFinishRecording:successfully:
委託方法調用和該標誌指示它是成功的。
我完全喪失了這些最後秒鐘如何丟失。 (該記錄沒有被釋放,仍然有很強的參考吧。)
UPDATE
該應用程序會提示用戶講多次,所以AVAudioRecorder正在之前暫停和恢復多次在最終迴應結束時停止。它暫停而不是停止,因爲它全部需要記錄到單個音頻文件。
我只做一個錄音,所以我沒有試圖使用相同的AVAudioRecorder實例進行多個錄音。另外,'recorder.currentTime'只能在if(recorder.isRecording)條件內被訪問。 – TimOfTheYear
爲了更好地幫助您,請在上下文中提供其他代碼。您如何錄製/暫停/停止錄音機,以及您在檢查'currentTime'的位置?如果在主線程(isMainThread)等處執行'stop',並且請在上面的答案中注意__EDIT__。 – Ashok