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) You should create/init AVAudioRecorder every time you begin a new recording.
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.
}
EDIT:
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);