我正在研究音乐音频。我想更改滑块值,同样音频值也会改变。
var avPlayer:AVPlayer!
var avPlayerItem:AVPlayerItem!
if let audioUrl = URL(string: self.idMusicFile) {
let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// destination file url
let destinationUrl = documentsDirectoryURL.appendingPathComponent(audioUrl.lastPathComponent)
print("destinationUrl is :",destinationUrl)
self.avPlayerItem = AVPlayerItem(url: destinationUrl)
self.avPlayer=AVPlayer(playerItem: self.avPlayerItem!)
self.avPlayer?.play()
}
var updater : CADisplayLink! = nil
self.updater = CADisplayLink(target: self, selector: #selector(NearByViewController.trackAudio))
self.updater.add(to: RunLoop.current, forMode: RunLoopMode.commonModes)
@objc func trackAudio() {
// if self.avPlayer.currentTime() = ""{}
let fraction = CMTimeGetSeconds((self.avPlayer?.currentTime())!) / CMTimeGetSeconds((self.avPlayer?.currentItem!.duration)!)
print("fraction",fraction)
self.sliderValue.value = Float(fraction)
}
此代码正在播放音频可以任何人建议我如何实现滑块更改然后音频也根据滑块值移动