I am recording audio using AVAudioRecorder
,and now i want to get exact time duration of my recorded audio,how can i get that.
i have tried this:
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:avAudioRecorder.url options:nil];
CMTime time = asset.duration;
double durationInSeconds = CMTimeGetSeconds(time);
But my time
variable return NULL and durationInSeconds
return 'nan',what does that means by nan.
UPDATE
user1903074
answer have solved my problem but just for curiosity ,is thair any way to do it without AVAudioplayer
.
The ones that you have made in the past only show the date because they are more than 24 hours old. Any voice memos that you make today Will show the time only until tomorrow.
A couple of the answers here use the same really poor time formatting that gives results like "0.1" for 1 second elapsed or "0.60" for 1 minute. If you want something that gives you a normal looking time like 0:01 or 1:00 then use this:
let minutes = Int(audioRecorder.currentTime / 60)
let seconds = Int(audioRecorder.currentTime) - (minutes * 60)
let timeInfo = String(format: "%d:%@%d", minutes, seconds < 10 ? "0" : "", seconds)
If you are using AVAudioPlayer
with AVAudioRecorder
than you can get the audioPlayer.duration
and get the time.
like this.
NSError *playerError;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:yoururl error:&playerError];
NSlog(@"%@",audioPlayer.duration);
But only if you are using AVAudioPlayer
with AVAudioRecorder
.
UPDATE
Or you can do like this.
//put this where you start recording
myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
// a method for update
- (void)updateTime {
if([recorder isRecording])
{
float minutes = floor(recorder.currentTime/60);
float seconds = recorder.currentTime - (minutes * 60);
NSString *time = [[NSString alloc]
initWithFormat:@"%0.0f.%0.0f",
minutes, seconds];
}
}
steel you can get some delay becouse their is some microsecond value,and i dont know how to clip it .but thats all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With