AV-自定义播放器

改编自oc版本的播放器

使用到的类主要有:

AVPlayerItem:

AVPlayer:播放器

AVPlayerLayer:预览页


核心方法:

let url = URL.init(string: "")

let playerItem =AVPlayerItem.init(url: url!)

let player =AVPlayer.init(playerItem: playerItem)

let playerLayer =AVPlayerLayer.init(player: player)

playerLayer.frame =view.frame

view.layer.addSublayer(playerLayer)

player.play()

切换视频:player.replaceCurrentItem(with: <#T##AVPlayerItem?#>)

停止播放:player.pause()

调整进度:player.seek(to: <#T##CMTime#>)

监听播放进度:

playerItem.addObserver(self, forKeyPath:"status", options: .new, context:nil)

监听缓存进度:

playerItem.addObserver(self, forKeyPath:"loadedTimeRanges", options: .new, context:nil)

监听播放状态:

NotificationCenter.default.addObserver(self, selector: #selector(self.playbackFinished(noti:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)


Demo 链接:https://github.com/licl19/AVPlayerDemo.git

你可能感兴趣的:(AV-自定义播放器)