美文网首页
AV-自定义播放器

AV-自定义播放器

作者: 流年易逝_李 | 来源:发表于2018-04-06 17:11 被阅读16次

    改编自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-自定义播放器

          本文链接:https://www.haomeiwen.com/subject/hzsshftx.html