语言版本: Swift4.2
先看一下效果图吧
简单播放器效果图
核心是AVPlayer,先用以下代码实现简单的视频播放
self.view.addSubview(displayView)
guard let url = URL(string: self.videoUrl) else {
return
}
let videoAsset = AVAsset.init(url: url)
let videoPlayItem = AVPlayerItem(asset: videoAsset)
self.player = AVPlayer(playerItem: videoPlayItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = displayView.frame
displayView.layer.addSublayer(playerLayer)
然后就是对player的一些操作
self.player?.pause() //暂停
self.player?.play() // 播放
接下来就是播放时跳转到某一位置,我是通过自定义的进度条和滑动手势来做的实现
首先,视频一边播放小滑块需要实时滑动,通过以下代码实现
// 设置控制器数值
let interval: CMTime = CMTime(seconds: 1.0, preferredTimescale: CMTimeScale.init(1))
self.playerObserve = self.player?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using: { [weak self] (cmTime) in
if (self?.isSliding ?? false) == false {
let secondNow = CMTimeGetSeconds(cmTime)
self?.allTime = CMTimeGetSeconds(videoPlayItem.asset.duration)
guard let theAllTime = self?.allTime else {
return
}
self?.leftTimeLabel.text = self?.formatPlayerTime(sourceTime: secondNow)
self?.rightTimeLabel.text = self?.formatPlayerTime(sourceTime: theAllTime)
let p = Float(secondNow / theAllTime)
self?.videoProgressView.progress = p
guard let thePoint = self?.sliderOrigiPoint,
let theSize = self?.sliderSize,
let theprogressWidth = self?.progressWidth else {
return
}
self?.sliderView.frame = CGRect(x: thePoint.x + CGFloat(p) * theprogressWidth , y: thePoint.y, width: theSize.width, height: theSize.height)
}
})
其中需要注意的就是全局变量isSliding, 当用户在滑动开始的时候停止视频播放,滑动结束时在新的位置播放,具体如下:
lazy var sliderView: UIImageView = {
let a = UIImageView()
a.image = UIImage(named: "sp_370_point", in: Bundle(for: self.classForCoder), compatibleWith: nil)
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(dragSlider(sender:)))
a.isUserInteractionEnabled = true
a.isMultipleTouchEnabled = true
a.addGestureRecognizer(panGesture)
return a
}()
拖动时操作
@objc private func dragSlider(sender: UIPanGestureRecognizer) {
switch sender.state {
case .began:
lastLocation = sliderView.center
self.player?.pause()
self.isSliding = true
case .ended:
self.isSliding = false
// 计算出进度,跳转进度
let p = (self.sliderView.frame.origin.x - self.sliderOrigiPoint.x) / self.progressWidth
let cmTime = CMTime(seconds: Double(p) * self.allTime, preferredTimescale: CMTimeScale(1))
self.player?.seek(to: cmTime)
self.player?.play()
default:
let translation = sender.translation(in: self.view)
let newCenterX = lastLocation.x + translation.x
let judgeX = newCenterX - self.sliderSize.width / 2
guard judgeX >= self.sliderOrigiPoint.x else {
sliderView.center = CGPoint(x: self.sliderOrigiPoint.x + (self.sliderSize.width / 2), y: lastLocation.y)
return
}
guard judgeX <= self.sliderOrigiPoint.x + self.progressWidth else {
sliderView.center = CGPoint(x: self.sliderOrigiPoint.x + self.progressWidth + (self.sliderSize.width / 2), y: lastLocation.y)
return
}
sliderView.center = CGPoint(x: lastLocation.x + translation.x, y: lastLocation.y)
}
}
网友评论