参考:Swift AVPlayer 播放网络视频之基础篇
-
AVPlayerItem
媒体资源管理对象
管理视频的基本信息和状态,如进度,缓存进度等,一个
AVPlayerItem
对应一个视频资源
监听事件的处理
playItem = AVPlayerItem(url: url)
playItem?.addObserver(self, forKeyPath: "CMTime", options: .new, context: nil)
playItem?.addObserver(self, forKeyPath: "status", options: .new, context: nil)
//一共有三种状态 Unknown 、ReadyToPlay 、 Failed 只有在 ReadyToPlay 状态下视频才能播放。
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "CMTime" {
print("缓存进度")
}else if keyPath == "status" {
if playItem?.status == .readyToPlay {
player?.play()
print("xxx 正常加载")
}else {
print("加载异常")
}
}
}
-
AVPlayer
视频操作对象
视频操作对象,但是无法显示视频,需要加到
AVPlayerLayer
上
-
AVPlayerLayer
用来显示视频的
确定显示视频的frame
//简单的视频播放
let url = URL(string: "http://bos.nj.bpc.baidu.com/tieba-smallvideo/11772_3c435014fb2dd9a5fd56a57cc369f6a0.mp4")
let videoItem = AVPlayerItem(url: url!)
let videoPlay = AVPlayer(playerItem: videoItem)
videoPlay.rate = 2// 1 正常播放速度, 0 暂停
let videoLayer = AVPlayerLayer(player: videoPlay)
videoLayer.frame = CGRect(x: 0, y: 50, width: UIScreen.main.bounds.size.width, height: 300)
view.layer.addSublayer(videoLayer)
videoPlay.play()
-
CADisplayLink
的执行次数相当于屏幕的帧数,iPhone
不卡顿的时候是每秒60
次。
let link = CADisplayLink(target: self, selector: #selector(update))
link.add(to: RunLoop.main, forMode: .defaultRunLoopMode)
func update() {
let currentTime = CMTimeGetSeconds((player?.currentTime())!)
let totalTime = TimeInterval((playItem?.duration.value)!) / TimeInterval((playItem?.duration.timescale)!)
let timeStr = "\(formatPlayTime(secounds: currentTime))/\(formatPlayTime(secounds: totalTime))"
timeLabel.text = timeStr
slider.value = Float(currentTime/totalTime)
if !sliding {
slider.value = Float(currentTime/totalTime)
}
}
/** 时间格式转换 */
func formatPlayTime(secounds: TimeInterval) -> String {
if secounds.isNaN {
return "00:00"
}
let minit = Int(secounds/60)
let sec = Int(secounds.truncatingRemainder(dividingBy: 60))
return String(format: "%02d:%02d", minit, sec)
}
/** 滑动滑块 */
func sliderTouchDowm(slider:UISlider) {
sliding = true
if player?.status == AVPlayerStatus.readyToPlay {
let duration = slider.value * Float(CMTimeGetSeconds((player?.currentItem?.duration)!))
let seekTime = CMTime(value: CMTimeValue(duration), timescale: 1)
player?.seek(to: seekTime, completionHandler: { (b) in
self.sliding = false
})
}
}
CADisplayLink 在退出使用时需要
link?.invalidate()
否则会造成循环引用导致退出视频播放页面时该页面无法释放
//
// ViewController.swift
// TMYSegmentController
//
// Created by TMY on 2017/5/8.
// Copyright © 2017年 TMY. All rights reserved.
//
/**
播放网络视频...
*/
import UIKit
import AVFoundation
typealias completeClosure = (_ success:Bool, _ splitImgs: Array) -> ()
class ViewController: UIViewController {
private var playItem: AVPlayerItem?
private var player: AVPlayer?
private var playLayer: AVPlayerLayer?
private let timeLabel = UILabel()
private let slider = UISlider()
private var sliding = false
private var link: CADisplayLink?
override func viewDidLoad() {
super.viewDidLoad()
title = "视频播放"
view.backgroundColor = #colorLiteral(red: 0.7277651429, green: 0.738256216, blue: 0.978562057, alpha: 1)
guard let url = URL.init(string: "http://bos.nj.bpc.baidu.com/tieba-smallvideo/11772_3c435014fb2dd9a5fd56a57cc369f6a0.mp4") else {
fatalError("链接错误")
}
playItem = AVPlayerItem(url: url)
playItem?.addObserver(self, forKeyPath: "loadTimeRanges", options: .new, context: nil)
playItem?.addObserver(self, forKeyPath: "status", options: .new, context: nil)
player = AVPlayer(playerItem: playItem)
playLayer = AVPlayerLayer(player: player)
playLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
playLayer?.contentsScale = UIScreen.main.scale
playLayer?.frame = UIScreen.main.bounds
playLayer?.backgroundColor = #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1).cgColor
view.layer.addSublayer(playLayer!)
bottomTools()
}
func bottomTools() {
timeLabel.do {
$0.textColor = #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1)
$0.text = "xxx"
$0.font = UIFont.systemFont(ofSize: 14)
$0.frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height - 20, width: 100, height: 25)
}
view.addSubview(timeLabel)
link = CADisplayLink(target: self, selector: #selector(update))
link?.add(to: RunLoop.main, forMode: .defaultRunLoopMode)
slider.do {
$0.minimumValue = 0
$0.maximumValue = 1
$0.value = 0
$0.maximumTrackTintColor = UIColor.cyan
$0.minimumTrackTintColor = UIColor.white
$0.setThumbImage(#imageLiteral(resourceName: "my_icon_info.png"), for: .normal)
$0.frame = CGRect(x: 10, y: UIScreen.main.bounds.size.height - 20, width: UIScreen.main.bounds.size.width - 110, height: 25)
$0.addTarget(self, action: #selector(sliderTouchDowm(slider:)), for: .touchDown)
$0.addTarget(self, action: #selector(sliderTouchDowm(slider:)), for: .touchUpOutside)
$0.addTarget(self, action: #selector(sliderTouchDowm(slider:)), for: .touchUpInside)
$0.addTarget(self, action: #selector(sliderTouchDowm(slider:)), for: .touchCancel)
}
timeLabel.frame = CGRect(x: slider.frame.maxX + 10, y: UIScreen.main.bounds.size.height - 20, width: 100, height: 25)
view.addSubview(slider)
}
func formatPlayTime(secounds: TimeInterval) -> String {
if secounds.isNaN {
return "00:00"
}
let minit = Int(secounds/60)
let sec = Int(secounds.truncatingRemainder(dividingBy: 60))
return String(format: "%02d:%02d", minit, sec)
}
func update() {
let currentTime = CMTimeGetSeconds((player?.currentTime())!)
let totalTime = TimeInterval((playItem?.duration.value)!) / TimeInterval((playItem?.duration.timescale)!)
let timeStr = "\(formatPlayTime(secounds: currentTime))/\(formatPlayTime(secounds: totalTime))"
timeLabel.text = timeStr
slider.value = Float(currentTime/totalTime)
if !sliding {
slider.value = Float(currentTime/totalTime)
}
}
func sliderTouchDowm(slider:UISlider) {
sliding = true
if player?.status == AVPlayerStatus.readyToPlay {
let duration = slider.value * Float(CMTimeGetSeconds((player?.currentItem?.duration)!))
let seekTime = CMTime(value: CMTimeValue(duration), timescale: 1)
player?.seek(to: seekTime, completionHandler: { (b) in
self.sliding = false
})
}
}
func sliderTouchUpOut(slider: UISlider) {
print("xxxxxxxxxxxx touchUPout, insider, cancel")
}
//一共有三种状态 Unknown 、ReadyToPlay 、 Failed 只有在 ReadyToPlay 状态下视频才能播放。
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "CMTime" {
print("缓存进度")
}else if keyPath == "status" {
if playItem?.status == .readyToPlay {
player?.play()
print("xxx 正常加载")
}else {
print("加载异常")
}
}
}
override func viewDidDisappear(_ animated: Bool) {
print("video is viewDidDisappear or not")
link?.invalidate()//退出页面的时候需停止CADisplayLink 避免循环引用
link = nil
}
deinit {
print("video is deinit or not")
playItem?.removeObserver(self, forKeyPath: "loadTimeRanges")
playItem?.removeObserver(self, forKeyPath: "status")
playItem = nil
playLayer = nil
player = nil
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Demo下载