如何在iphone中获取录制音频的时间? [英] How can i get time of my recorded audio in iphone?
问题描述
我正在使用 AVAudioRecorder
录制音频,现在我想获得录制音频的准确持续时间,我该怎么做。
I am recording audio using AVAudioRecorder
,and now i want to get exact time duration of my recorded audio,how can i get that.
我试过这个:
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:avAudioRecorder.url options:nil];
CMTime time = asset.duration;
double durationInSeconds = CMTimeGetSeconds(time);
但我的时间
变量返回NULL和 durationInSeconds
返回'nan',这对nan来说意味着什么。
But my time
variable return NULL and durationInSeconds
return 'nan',what does that means by nan.
UPDATE
user1903074
答案已经解决了我的问题,但仅仅是为了好奇,是没有 AVAudioplayer
。
user1903074
answer have solved my problem but just for curiosity ,is thair any way to do it without AVAudioplayer
.
推荐答案
如果您使用 AVAudioPlayer
AVAudioRecorder
比你可以得到 audioPlayer.duration
并得到时间。
If you are using AVAudioPlayer
with AVAudioRecorder
than you can get the audioPlayer.duration
and get the time.
喜欢这个。
NSError *playerError;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:yoururl error:&playerError];
NSlog(@"%@",audioPlayer.duration);
但仅当您使用 AVAudioPlayer
时 AVAudioRecorder
。
更新
或者你可以这样做。
//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.
这篇关于如何在iphone中获取录制音频的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!