我有两个UIViewControllers,每个都有一个UISlider
。一个滑块跟踪从AVPlayer
播放的音频文件的进度,另一个视图控制器滑块跟踪从AVAudioPlayer
播放的音频文件的进度。响应AVPlayer
的滑块动画完全平滑,而滑块响应AVAudioPlayer
的动画不平滑。滑块设置完全相同,除了我如何获得每个玩家的当前时间,如下所示:
//AVPlayer slider
func trackAudio() {
let currentTime = self.newPlayer!.currentTime()
let currentTimeInSeconds = CMTimeGetSeconds(currentTime)
slider.setValue(Float(currentTimeInSeconds), animated: true)
}
//AVAudioPlayer Slider
func trackAudio() {
let currentTime = self.player!.currentTime
slider.setValue(Float(currentTime), animated: true)
}
func playPressed(){
player!.play()
updater = CADisplayLink(target: self, selector: #selector(RecordController.trackAudio))
updater.add(to: RunLoop.current, forMode: RunLoopMode.commonModes)
}
答案 0 :(得分:0)
UISlider不平滑的原因是因为我在同一个视图控制器中有两个CADisplayLink。
我最初有以下内容:
>>> "{}".format(f)
'3.65'
>>> "{:.10f}".format(f)
'3.6500000000'
>>> "{:.20f}".format(f)
'3.64999999999999991118'
我通过以下方式开始工作:
var displayLink: CADisplayLink? // for recording animation
var updater: CADisplayLink? // for updating slider
func record() {
let displayLink = CADisplayLink(target: self, selector:#selector(RecordController.updateMeters))
displayLink.add(to: RunLoop.current, forMode: RunLoopMode.commonModes)
}
func play() {
let updater = CADisplayLink(target: self, selector: #selector(RecordController.trackAudio))
updater.add(to: RunLoop.current, forMode: RunLoopMode.commonModes)
}