11

我正在使用录制音频AVAudioRecorder,现在我想获得录制音频的确切持续时间,我该如何获得。

我试过这个:

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:avAudioRecorder.url options:nil];
CMTime time = asset.duration;
double durationInSeconds = CMTimeGetSeconds(time);

但是我的time变量返回NULL并durationInSeconds返回'nan',nan是什么意思。

更新

user1903074答案已经解决了我的问题,但只是出于好奇,没有任何方法可以做到这一点AVAudioplayer

4

6 回答 6

16

如果您使用AVAudioPlayerwith AVAudioRecorder,则可以获得audioPlayer.duration并获得时间。

像这样。

 NSError *playerError;

 audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:yoururl error:&playerError];

 NSlog(@"%@",audioPlayer.duration);

但仅当您使用AVAudioPlayerwith 时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];
    }
}

钢你可以得到一些延迟,因为它们是一些微秒值,我不知道如何剪辑它。但仅此而已。

于 2012-12-14T05:23:02.393 回答
2

我的解决方案是简单地跟踪开始日期,最后计算经过的时间。它对我有用,因为用户正在启动和停止录音机。

在录音机类

NSDate* startTime;
NSTimeInterval duration;

-(void) startRecording
{
   //start the recorder
   ...
   duration = 0;
   startTime = [NSDate date];

}

-(void) stopRecording
{
   //stop the recorder
   ...
   duration = [[NSDate date] timeIntervalSinceDate:startRecording];
}
于 2014-05-29T10:07:43.980 回答
1

这里的几个答案使用相同的非常糟糕的时间格式,给出的结果如“0.1”持续 1 秒或“0.60”持续 1 分钟。如果你想要一些能让你看起来像 0:01 或 1:00 这样的正常时间的东西,那么使用这个:

let minutes = Int(audioRecorder.currentTime / 60)
let seconds = Int(audioRecorder.currentTime) - (minutes * 60)
let timeInfo = String(format: "%d:%@%d", minutes, seconds < 10 ? "0" : "", seconds)
于 2021-05-25T14:10:17.960 回答
0

我知道最初的问题是在 Objective-C 中寻找答案,但对于那些希望在 Swift 中这样做的人来说,这适用于 Swift 4.0:

let recorder = // Your instance of an AVAudioRecorder
let player = try? AVAudioPlayer(contentsOf: recorder.url)
let duration = player?.duration ?? 0.0

假设您有一个有效的记录器,那么您将获得一个持续时间值,它以TimeInterval.

于 2018-02-01T07:11:43.917 回答
0

更新@Dilip 对 Swift 5.3 的出色回答:

//put this where you start recording
myTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)


// a method for update
 @objc func updateTime(){
     if recorder.isRecording{
         let minutes = floor(recorder.currentTime/60)
         let seconds = recorder.currentTime - (minutes * 60)
         let timeInfo = String(format: "%0.0f.%0.0f", minutes, seconds)
     }
 }

这应该与

    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(AVAudioSession.Category.playAndRecord, options: AVAudioSession.CategoryOptions.defaultToSpeaker)
        audioSession.requestRecordPermission({ (isGranted: Bool) in
            
        })
    } catch  {}
于 2020-12-17T10:23:03.750 回答
0

在停止录制之前,请尝试获取 .currentTime

let duration = audioRecorder.currentTime
audioRecorder.stop()
于 2022-01-02T14:08:49.037 回答