2013-12-16 26 views
0

我用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正在之前暫停和恢復多次在最終迴應結束時停止。它暫停而不是停止,因爲它全部需要記錄到單個音頻文件。

回答

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. 

} 

編輯:

使用[self.audioRecorder stop]或內部audioRecorderDidFinishRecording代表stop記錄後您的來電方法,請使用下面的代碼(基於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); 
+0

我只做一個錄音,所以我沒有試圖使用相同的AVAudioRecorder實例進行多個錄音。另外,'recorder.currentTime'只能在if(recorder.isRecording)條件內被訪問。 – TimOfTheYear

+0

爲了更好地幫助您,請在上下文中提供其他代碼。您如何錄製/暫停/停止錄音機,以及您在檢查'currentTime'的位置?如果在主線程(isMainThread)等處執行'stop',並且請在上面的答案中注意__EDIT__。 – Ashok