AVPlayer

AVPlayer

AVPlayer is intended for playing a single media asset at a time. The player instance can be reused to play additional media assets using its replaceCurrentItem(with:) method, but it manages the playback of only a single media asset at a time. The framework also provides a subclass of AVPlayer, called AVQueuePlayer, that you can use to create and manage of queue of media assets to be played sequentially.
一个本地、远程音视频播放器,简单好用

//AVAsset (本地路径或者网络链接)
let asset = AVAsset.init(url: URL.init(fileURLWithPath: filePath))

//AVPlayerItem
let playItem = AVPlayerItem.init(asset: asset, automaticallyLoadedAssetKeys: ["tracks","duration","commonMetadata"])

//AVPlayer
let player = AVPlayer.init(playerItem: playItem)
//监听实时进度
player.addPeriodicTimeObserver(forInterval: CMTime.init(value: 1, timescale: 1), queue: DispatchQueue.main) { (time) in
            print("time = \(time.value)")
}

//AVPlayerLayer(添加到内容区的layer)
let playerLayer = AVPlayerLayer.init(player: player)
playerLayer.frame = view.bounds
view.layer.addSublayer(playerLayer)

//监听视频加载状态变化
playItem.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.new, context: &playerContext)
//监听视频播放进度
NotificationCenter.default.addObserver(self, selector: #selector(handleItemFinish), name: Notification.Name.AVPlayerItemDidPlayToEndTime, object: playItem)

监听视频加载状态变化

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if let playItem = object as? AVPlayerItem{
            switch playItem.status{
            case .readyToPlay:
                player.play()
                player.addPeriodicTimeObserver(forInterval: CMTimeMake(1, 1), queue: DispatchQueue.global()) { (time) in
                }
            default:
                print("not readyToPlay")
            }
        }
    }

监听视频播放进度

@objc func handleItemFinish(notification:Notification){
        player.seek(to: kCMTimeZero)
}

音量条

let voloumView = MPVolumeView.init(frame: CGRect.init(x: (view.frame.size.width-200)*0.5, y: 450, width: 200, height: 100))
voloumView.showsRouteButton = false
voloumView.showsVolumeSlider = true
voloumView.sizeToFit()
view.addSubview(voloumView)

for subview in voloumView.subviews{
    if NSStringFromClass(subview.classForCoder) == "MPVolumeSlider"{
        if let slider = subview as? UISlider{
            slider.sendActions(for: UIControlEvents.touchUpInside)
            slider.thumbTintColor = UIColor.yellow;
        }
    }
}

参考链接:AVPlayer

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,504评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,150评论 0 23
  • 我从来没想过会变的像现在这样, 那么浓烈的热情不知道被什么冲淡。 可能就是一次次的怠慢和不及时的回应吧。 我再也不...
    刘勰阅读 403评论 1 1
  • 初夏日烈,小树稀荫,刺密伤多,汗浸撒盐,山高路远,身心疲倦,中途竟返,另辟近途,虽近无径...
    孤石刘琳锋阅读 116评论 0 0
  • 盆栽人一一吉中 花中真君子 笑给赏花人 书写
    吉羊玉奕v阅读 308评论 0 3