import UIKit
import AVFoundation
class PlayerViewController: UIViewController {
@IBOutlet weak var playButton: UIButton!
@IBOutlet weak var finishView: UIView!
@IBOutlet weak var backButton: UIButton!
@IBOutlet weak var bottomView: UIView!
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var bottomConatraint: NSLayoutConstraint!
@IBOutlet weak var topConatraint: NSLayoutConstraint!
//MARK: - 属性
//播放路径
var playUrlStr = ""
//播放状态
var isPlaying = true
//是否已经加载成功
var isLoadFinish = false
//播放器
lazy var player:AVPlayer = {
//创建播放源
let item = AVPlayerItem.init(URL: NSURL.init(string: self.playUrlStr)!)
//print("播放源\(self.playUrlStr)")
//创建播放器
let tplayer = AVPlayer.init(playerItem: item)
//添加观察者观察播放源中loadedTimeRanges属性的值的改变
//给指定对象的指定属性添加观察者,观察对象属性的值的改变
//(想要观察哪个对象的属性,就用哪个对象去调用addObserver方法),所有继承自NSOject的类都有addObserver方法
//参数1:观察者(谁去观察对象的属性的改变)
//参数2:对象的属性地址 person.name -> name对应的keyPath就是("name"),person.car.color,->color对应keypath就是("car.color")
//参数3:当值发生改变的时候,观察者需要获取的值的类型(旧值/新值)
//参数4:上下文(nil)
item.addObserver(self, forKeyPath: "loadedTimeRanges", options: .New, context: nil)
//获取播放结束的时刻
NSNotificationCenter.defaultCenter().addObserver(self, selector: "endPlayAction", name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
return tplayer
}()
//MARK: - 生命周期
override func viewDidLoad() {
super.viewDidLoad()
//创建播放界面
self.creatPlayerLayer()
//开始播放
self.player.play()
//获取播放进度
self.getPlayProgress()
}//函数结束
}
//MARK: - 观察者
extension PlayerViewController{
//当观察者观察的对象的属性的值发生改变后会自动调用的方法
//参数1:被观察的属性的路径
//参数2:被观察的对象
//参数3:变化的值(旧值/新值)
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
//获取当前播放源的总时间
let duraction = self.player.currentItem?.duration
let totalTime = Float(duraction!.value) / Float(duraction!.timescale)
//获取当前的缓存时间
//1.获取播放源对象
let item = object as! AVPlayerItem
//2.获取缓存进度对象
let value = item.loadedTimeRanges.first
let timeRange = value?.CMTimeRangeValue
//3.获取当前缓存到的时间
let current = timeRange?.duration
let currentTime = Float((current?.value)!) / Float((current?.timescale)!)
print(currentTime)
if currentTime > 1 {
self.isLoadFinish = true
}
}
}
//MARK: - 播放功能相关
extension PlayerViewController{
func endPlayAction() {
//1.改变按钮的显示状态
self.playButton.setImage(UIImage.init(named: "page_play_normal.png"), forState: .Normal)
self.isPlaying = false
//2.显示提示信息
self.finishView.hidden = false
}
func getPlayProgress(){
//获取播放进度
self.player.addPeriodicTimeObserverForInterval(CMTimeMake(1, 1), queue: dispatch_get_global_queue(0, 0)) { (currentTime) in
//1.计算时间(秒)
//当前时间
let current = CGFloat(currentTime.value) / CGFloat(currentTime.timescale)
let currentStr = ToolManager.transformTime(current)
//总时间
let duraction = CGFloat((self.player.currentItem?.duration.value)!) / CGFloat((self.player.currentItem?.duration.timescale)!)
let duractionStr = ToolManager.transformTime(duraction)
//2.计算进度
let progress = current/duraction
//回到主线程刷新数据
dispatch_async(dispatch_get_main_queue(), {
self.timeLabel.text = "\(currentStr) / \(duractionStr)"
self.slider.value = Float(progress)
})
}
}
}
//MARK: - 界面相关
extension PlayerViewController{
func creatPlayerLayer(){
//创建layer
let playLayer = AVPlayerLayer.init(player: self.player)
playLayer.frame = self.view.bounds
self.view.layer.addSublayer(playLayer)
//改变层次
self.view.bringSubviewToFront(self.backButton)
self.view.bringSubviewToFront(self.bottomView)
self.view.bringSubviewToFront(self.finishView)
}
}
//MARK: - 按钮点击
extension PlayerViewController{
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if bottomConatraint.constant == 0 {
UIView.animateWithDuration(0.3, animations: {
self.bottomConatraint.constant = -50
self.topConatraint.constant = -50
self.view.layoutIfNeeded()
})
}else{
UIView.animateWithDuration(0.3, animations: {
self.bottomConatraint.constant = 0
self.topConatraint.constant = 10
self.view.layoutIfNeeded()
})
}
}
@IBAction func playAction2(sender: AnyObject) {
//重新播放
self.slider.value = 0
self.playButton.setImage(UIImage.init(named: "pauseBtn.png"), forState: .Normal)
self.isPlaying = true
self.finishView.hidden = true
self.player.seekToTime(CMTimeMake(0, 10))
self.player.play()
}
@IBAction func sliderAction(sender: UISlider) {
//先判断当前视频是否已经加载成功
if self.isLoadFinish == false {
sender.value = 0
return
}
//拿到进度值
let progress = slider.value
//获取总的时间(秒)
let time = Float((self.player.currentItem?.duration.value)!) / Float((self.player.currentItem?.duration.timescale)!)
//计算当前时间
let current = Int64(time*progress)
//设置进度
self.player.seekToTime(CMTimeMake(current, 1))
}
//播放/暂停
@IBAction func playAction(sender: UIButton) {
//如果正在播放
if self.isPlaying{
//暂停播放
self.player.pause()
self.isPlaying = false
//改变按钮的显示状态
sender.setImage(UIImage.init(named: "page_play_normal.png"), forState: .Normal)
}else{
//开始播放
self.player.play()
self.isPlaying = true
//改变按钮的显示状态
sender.setImage(UIImage.init(named: "pauseBtn.png"), forState: .Normal)
}
}
//返回
@IBAction func backAction(sender: UIButton) {
//1.停止播放
self.player.pause()
//2.移除观察者
//移除self对播放源中loadedTimeRanges的观察
self.player.currentItem?.removeObserver(self, forKeyPath: "loadedTimeRanges")
self.dismissViewControllerAnimated(true, completion: nil)
}
}
//MARK: - 横屏相关
extension PlayerViewController{
//是否支持屏幕旋转
override func shouldAutorotate() -> Bool {
return true
}
//设置屏幕的方向
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
//横屏,home键在右
return .LandscapeRight
}
}
AVPlayer代码解析
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 内核源代码可以从kernel.org上下载 。kernel upstream 提供了长期维护的LTS内核版本。 目...
- Netty是一个java的高性能同步/异步通讯框架,基于SEDA模型。最近因为要逐渐接触java项目,就看了下它的...
- 前言 DOM是很慢的。真正的 DOM 元素非常庞大,这是因为标准就是这么设计的。而且操作它们的时候你要小心翼翼,轻...
- 浮于表面探究问题不失为一种方法,但是弄清楚本质才是真正意义上的解决疑惑。 之前写的一篇博客object_getCl...