改編自oc版本的播放器
使用到的類主要有:
AVPlayerItem:
AVPlayer:播放器
AVPlayerLayer:預(yù)覽頁
核心方法:
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()
調(diào)整進(jìn)度:player.seek(to: <#T##CMTime#>)
監(jiān)聽播放進(jìn)度:
playerItem.addObserver(self, forKeyPath:"status", options: .new, context:nil)
監(jiān)聽緩存進(jìn)度:
playerItem.addObserver(self, forKeyPath:"loadedTimeRanges", options: .new, context:nil)
監(jiān)聽播放狀態(tài):
NotificationCenter.default.addObserver(self, selector: #selector(self.playbackFinished(noti:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)