首页 文章

以秒为单位播放AVPlayerItem CurrentTime会返回不一致的值

提问于
浏览
0

我正在重新调整我的音频播放器应用程序以使用AVQueuePlayer而不是AVAudioPlayer,因为我现在感谢我需要能够排队音频文件以进行播放 . 由于AVQueuePlayer是AVPlayer的子类,它使用CMTime来跟踪AVPlayerItem时间属性(持续时间,当前时间等等),我不得不重新设计动画功能,更新我的AudioPlayer视图的滑块和UILabels .

我发现,现在我使用的是CMTime结构,而不是TimeIntervals,我的currentSeconds变量会在播放音频文件时返回一些奇怪的,不一致的值 . 您可以在我已包含的代码下方看到打印结果 .

这也导致我的轨道滑块打嗝到不同的点,持续时间标签跳到奇数值,包括00:00,同时回放一半 .

我对这里发生的很多事情都很陌生,我试图改变addTimeObserver函数中的间隔值,认为但是它没有改变任何东西 . 这可能是我计划每隔0.1秒触发animateFooterView()的Timer对象的问题......但我不知道如何解决它 .

func animateFooterView(){

    let track = audioQueuePlayer?.currentItem
    let duration: CMTime = (track?.asset.duration)!
    let durationSeconds: Float64 = CMTimeGetSeconds(duration)
    let currentSeconds: Float64 = CMTimeGetSeconds(track!.currentTime())

    audioQueuePlayer?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 10), queue: DispatchQueue.main, using:
        { (CMTime) -> Void in
            if track?.status == .readyToPlay{

                let trackProgress: Float = Float(currentSeconds / durationSeconds) //* (self.footerView.trackSlider.maximumValue)

                print("Progress in seconds: " + String(currentSeconds))

                self.footerView.trackSlider.value = trackProgress

                let minutesProgress = Int(currentSeconds) / 60 % 60
                let secondsProgress = Int(currentSeconds) % 60

                let minutesDuration = Int(durationSeconds) / 60 % 60
                let secondsDuration = Int(durationSeconds) % 60

                self.footerView.durationLabel.text = String(format: "%02d:%02d | %02d:%02d", minutesProgress, secondsProgress, minutesDuration, secondsDuration)
            }
        })
}

Print() output of currentSeconds:

Progress in seconds: 0.959150266
Progress in seconds: 0.459939791
Progress in seconds: 0.739364115
Progress in seconds: 0.0
Progress in seconds: 0.0
Progress in seconds: 0.253904687
Progress in seconds: 0.0
Progress in seconds: 0.15722927
Progress in seconds: 0.346783126
Progress in seconds: 0.050562017
Progress in seconds: 1.158813019
Progress in seconds: 1.252108811
Progress in seconds: 1.356793865
Progress in seconds: 1.458337661
Progress in seconds: 1.554249846
Progress in seconds: 1.615820093
Progress in seconds: 0.0
Progress in seconds: 0.0
Progress in seconds: 0.0
Progress in seconds: 0.050562017
Progress in seconds: 0.15722927
Progress in seconds: 0.253904687
Progress in seconds: 0.346783126
Progress in seconds: 0.459939791
Progress in seconds: 0.558436703
Progress in seconds: 0.656613436
Progress in seconds: 0.739364115
Progress in seconds: 0.854647401
Progress in seconds: 0.959150266
Progress in seconds: 1.057932049
Progress in seconds: 1.158813019

以下是我提到的计时器的一瞥,每当footerView播放时,它每0.1秒调用一次animateFooterView .

func play() {

   ...
   timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(animateFooterView), userInfo: nil, repeats: true)      
   ...
}

实际上,我想我刚刚意识到这里出了什么问题......由于计时器触发,我每0.1秒添加一个periodicTimeObserver . 我想我只需要添加一次观察者,然后一起取消计时器?

1 回答

  • 0

    我认为计时器不断向AVPlayer添加一个观察者会弄乱返回的当前时间,这是正确的 . 我删除了计时器,而是让观察者功能更新当前时间 . 它现在很完美 .

相关问题