功能最全、自定义最高的播放器,也是使用最多得。使用起来较为复杂些。需导入AVKit控件(import AVKit)
相关参数
- AVPlayer: 负责控制播放器的播放,暂停等操作
- AVAsset:获取多媒体信息的抽象类,不能直接使用
- AVURLAsset: AVAsset 的一个子类,使用 URL 进行实例化,实例化对象包换 URL 对应视频资源的所有信息。
- AVPlayerItem: 管理媒体资源对象,提供播放数据源
- AVPlayerLayer: 负责显示视频,如果没有添加该类,只有声音没有画面
下载demo

swift视频播放器使用
基本播放
播放设置
self.palyerItem = AVPlayerItem(url: NSURL(string: urlString)! as URL) self.player = AVPlayer.init(playerItem: self.palyerItem) self.player.rate = 1.0 let playerLayer = AVPlayerLayer.init(player: self.player) playerLayer.videoGravity = .resizeAspect playerLayer.frame = self.view.bounds self.view.layer .addSublayer(playerLayer) self.player.play()
|
播放暂停
func pauseButtonSelected(sender:UIButton) { sender.isSelected = !sender.isSelected if sender.isSelected{ self.player.pause() }else{ self.player.play() } }
|
观察AVPlayerItem属性(观察播放状态、缓存情况等)
当前播放状态
KVO观察当前的播放状态
self.palyerItem.addObserver(self, forKeyPath: "status", options: .new, context: nil)
|
一共有三种播放状态,准备播放、播放失败、未知。
func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if keyPath == "status" { switch self.palyerItem.status{ case .readyToPlay: self.play() case .failed: print("failed") case.unknown: print("unkonwn") } }
|
播放时间与总时间
处理时间,有一个结构体CMTime
typedef struct{ CMTimeValue value; CMTimeScale timescale; CMTimeFlags flags; CMTimeEpoch epoch; } CMTime;
|
视频总时间
let totalTime = CMTimeGetSeconds((self?.player.currentItem?.duration)!)
|
当前播放时间
let currentTime = self.playItem.currentTime.value/self.playItem.currentTime.timescale;
|
实时的获取播放时长和控制进度条
self.player.addPeriodicTimeObserver(forInterval: CMTimeMake(1, 1), queue: DispatchQueue.main) { [weak self](time) in let loadTime = CMTimeGetSeconds(time) let totalTime = CMTimeGetSeconds((self?.player.currentItem?.duration)!) self?.slider.value = Float(loadTime/totalTime) self?.loadTimeLabel.text = self?.changeTimeFormat(timeInterval: loadTime) self?.totalTimeLabel.text =self?.changeTimeFormat(timeInterval:CMTimeGetSeconds((self?.player.currentItem?.duraton)!)) }
|
播放缓存
与播放缓存相关的观测属性
- loadedTimeRanges 缓存区间,可用来获取缓存了多少
- playbackBufferEmpty 缓存不够了 自动暂停播放
- playbackLikelyToKeepUp 缓存好了 手动播放
self.palyerItem.addObserver(self, forKeyPath: "loadedTimeRanges", options: .new, context: nil) self.palyerItem.addObserver(self, forKeyPath: "playbackBufferEmpty", options: .new, context: nil) self.palyerItem.addObserver(self, forKeyPath:"playbackLikelyToKeepUp", options: .new,context:nil)
|
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if keyPath == "status" { switch self.palyerItem.status{ case .readyToPlay: self.play() case .failed: print("failed") case.unknown: print("unkonwn") } }else if keyPath == "loadedTimeRanges"{ let loadTimeArray = self.palyerItem.loadedTimeRanges let newTimeRange : CMTimeRange = loadTimeArray.first as! CMTimeRange let startSeconds = CMTimeGetSeconds(newTimeRange.start); let durationSeconds = CMTimeGetSeconds(newTimeRange.duration); let totalBuffer = startSeconds + durationSeconds; print("当前缓冲时间:%f",totalBuffer) }else if keyPath == "playbackBufferEmpty"{ print("正在缓存视频请稍等") } else if keyPath == "playbackLikelyToKeepUp"{ print("缓存好了继续播放") self.player.play() } }
|
要记得移除观察,而且这里是self.palyerItem,不要搞错了
self.palyerItem.removeObserver(self, forKeyPath: "status", context: nil) self.palyerItem.removeObserver(self, forKeyPath: "loadedTimeRanges", context: nil) self.palyerItem.removeObserver(self, forKeyPath: "playbackBufferEmpty", context: nil) self.palyerItem.removeObserver(self,forKeyPath: "playbackLikelyToKeepUp", context: nil) NotificationCenter.default.removeObserver(self)
|
AVplayer常用相关通知
AVAudioSessionInterruptionNotification AVAudioSessionSilenceSecondaryAudioHintNotification AVAudioSessionMediaServicesWereLostNotification AVAudioSessionMediaServicesWereResetNotification AVAudioSessionSilenceSecondaryAudioHintNotification
|
AVPlayerItemDidPlayToEndTimeNotification AVPlayerItemTimeJumpedNotification AVPlayerItemPlaybackStalledNotification AVPlayerItemFailedToPlayToEndTimeNotification
|
使用AVPlayerItemDidPlayToEndTime通知
NotificationCenter.default.addObserver(self, selector: #selector(playToEndTime), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil) @objc func playToEndTime(){ print("播放完成") }
|