Sou*_*Das 2 uislider ios avplayer
我正在使用自定义UISlider在AVPlayer中播放视频时实现擦洗.试图找出在UISlider各端显示当前播放时间和剩余持续时间的最佳方式,因为它通常显示在MPMoviePlayerController中.
任何帮助表示赞赏.
斯威夫特 4.x
let player = AVPlayer()
player.addPeriodicTimeObserver(forInterval: CMTime.init(value: 1, timescale: 1), queue: .main, using: { time in
if let duration = player.currentItem?.duration {
let duration = CMTimeGetSeconds(duration), time = CMTimeGetSeconds(time)
let progress = (time/duration)
if progress > targetProgress {
print(progress)
//Update slider value
}
}
})
Run Code Online (Sandbox Code Playgroud)
或者
extension AVPlayer {
func addProgressObserver(action:@escaping ((Double) -> Void)) -> Any {
return self.addPeriodicTimeObserver(forInterval: CMTime.init(value: 1, timescale: 1), queue: .main, using: { time in
if let duration = self.currentItem?.duration {
let duration = CMTimeGetSeconds(duration), time = CMTimeGetSeconds(time)
let progress = (time/duration)
action(progress)
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
用
let player = AVPlayer()
player.addProgressObserver { progress in
//Update slider value
}
Run Code Online (Sandbox Code Playgroud)
在每一端都放一个UILabel.使用更新它们-[AVPlayer addPeriodicTimeObserverForInterval:queue:usingBlock:].使用-[AVPlayer currentTime]和计算剩余时间-[AVPlayerItem duration].
| 归档时间: |
|
| 查看次数: |
5234 次 |
| 最近记录: |