AVAudioPlayer 获取音频时长不准确的解决办法

  1. AVAudioPlayer 通过播放音频发现AVAudioPlayer的duration属性会变,当快播放到最后的时候duration属性才是准确的,如下图:


    image.png

所以解决办法就是将AVAudioPlayer的currentTime设置到靠近最后位置,然后duration属性就是正常了

self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:&error];
    if (error) {
        NSLog(@"创建录音播放对象发生错误,错误信息是:%@",error.localizedDescription);
    }else {
        self.audioPlayer.delegate = self;
        self.audioPlayer.meteringEnabled = YES;
        self.audioPlayer.numberOfLoops = 0;
        [self.audioPlayer prepareToPlay];
        
        // 获取准确的时长
        NSLog(@"1.-----%f",self.audioPlayer.duration);
        _audioPlayer.currentTime = self.audioPlayer.duration;
        NSLog(@"2.-----%f",self.audioPlayer.duration);
        _audioPlayer.currentTime = 0;
        NSLog(@"3.-----%f",self.audioPlayer.duration);
    }

你可能感兴趣的:(AVAudioPlayer 获取音频时长不准确的解决办法)