继续..自定义播放器

This commit is contained in:
bluesea 2024-04-17 16:27:01 +08:00
parent c438d862cf
commit a73cb139a4
6 changed files with 110 additions and 15 deletions

View File

@ -32,8 +32,8 @@
endingColumnNumber = "9223372036854775807"
startingLineNumber = "576"
endingLineNumber = "576"
landmarkName = "CCHomeController"
landmarkType = "21">
landmarkName = "imagePickerController(_:didFinishPickingMediaWithInfo:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
@ -96,7 +96,7 @@
endingColumnNumber = "9223372036854775807"
startingLineNumber = "540"
endingLineNumber = "540"
landmarkName = "gotoVideoTransformVC(url:asset:)"
landmarkName = "photoLibrary()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>

View File

@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "icon@1x.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 741 KiB

View File

@ -13,7 +13,34 @@ class ZZHCustomPlayer: UIView {
var avPlayer:AVPlayer?
var avPlayerLayer:AVPlayerLayer?
var timeSlider:ZZHCustomSlider?
var prePlayingState:Bool?// ,player,便
let playerPauseBgColor:UIColor = UIColor(r: 245, g: 245, b: 245, a: 0.2)//
lazy var maskPlayerView:UIView? = {//
let bgView = UIView()
bgView.backgroundColor = playerPauseBgColor
bgView.addSubview(self.playStateImgView)
self.playStateImgView.snp.makeConstraints { make in
make.centerX.centerY.equalToSuperview()
make.width.height.equalTo(40)
}
let btn = UIButton()
bgView.addSubview(btn)
btn.addTarget(self, action: #selector(playOrPause(sender: )), for: .touchUpInside)
btn.snp.makeConstraints { make in
make.left.top.right.bottom.equalToSuperview()
}
return bgView
}()
lazy var playStateImgView:UIImageView = {
let imgView = UIImageView()
imgView.image = UIImage(named: "custom_playBtn")
return imgView
}()
override init(frame: CGRect) {
super.init(frame: frame)
@ -32,9 +59,14 @@ class ZZHCustomPlayer: UIView {
playerLayerBgView!.layer.addSublayer(avPlayerLayer!)
playerLayerBgView?.backgroundColor = UIColor.orange
self.maskPlayerView?.frame = avPlayerLayer!.frame
playerLayerBgView?.addSubview(maskPlayerView!)
timeSlider = ZZHCustomSlider(frame: .zero)
timeSlider?.processChangeValueCallback = { value in
print("value:\(value)")
timeSlider?.processChangeValueCallback = { (value,isMoving) in
print("value:\(value) isMoving:\(isMoving)")
}
self.addSubview(timeSlider!)
timeSlider!.snp.makeConstraints { make in
@ -47,7 +79,39 @@ class ZZHCustomPlayer: UIView {
}
//
@objc func playOrPause(sender:UIButton) {
if sender.tag == 0 {//
play(true)
sender.tag = 1
print("bofang...")
}
else {//
print("暂停...")
play(false)
sender.tag = 0
}
}
//使play
func play(_ value:Bool) {
if value {
self.avPlayer?.play()
prePlayingState = true
}
else {
prePlayingState = false
self.avPlayer?.pause()
}
updatePlayMaskView(value)
}
//
func updatePlayMaskView(_ value:Bool) {
playStateImgView.isHidden = value
self.maskPlayerView?.backgroundColor = value ? .clear : playerPauseBgColor
}
override func draw(_ rect: CGRect) {
super.draw(rect)

View File

@ -7,7 +7,7 @@
import Foundation
import UIKit
typealias CustomSliderProcessChangeValueCallback = (_ currentValue:Float)->Void
typealias CustomSliderProcessChangeValueCallback = (_ currentValue:Float,_ isMoving:Bool)->Void
class ZZHCustomSlider: UIView {
var sHeight:CGFloat?
var sLabel:UILabel?
@ -74,20 +74,27 @@ class ZZHCustomSlider: UIView {
}
//value:0~1
func updateProcessValue(value:Float) {
func updateProcessValue(value:Float,isMoving:Bool) {
self.timeSlider!.value = value
guard let processChangeValueCallback else {
return
}
var currentValue = max(0, min(value, 1))
//0.x
let formattedValue = String(format: "%.1f", currentValue)
currentValue = Float(formattedValue)!
if currentValue > preValue || currentValue < preValue {
preValue = currentValue
processChangeValueCallback(preValue)
if isMoving {
var currentValue = max(0, min(value, 1))
//0.x
let formattedValue = String(format: "%.1f", currentValue)
currentValue = Float(formattedValue)!
if currentValue > preValue || currentValue < preValue {
preValue = currentValue
processChangeValueCallback(preValue,isMoving)
}
}
else {
processChangeValueCallback(preValue,isMoving)
}
}
//
@ -108,7 +115,7 @@ class ZZHCustomSlider: UIView {
if let th = touch {
let touchPoint = th.location(in: self.timeSlider!)
updateProcessValue(value: Float(touchPoint.x / self.timeSlider!.frame.size.width))
updateProcessValue(value: Float(touchPoint.x / self.timeSlider!.frame.size.width),isMoving: true)
}
}
@ -118,6 +125,8 @@ class ZZHCustomSlider: UIView {
timeSlider!.isNormal = true
self.updateSliderHeight(height: (self.sHeight)!)
}
updateProcessValue(value: preValue,isMoving: false)
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
@ -125,6 +134,7 @@ class ZZHCustomSlider: UIView {
timeSlider!.isNormal = true
self.updateSliderHeight(height: (self.sHeight)!)
}
updateProcessValue(value: preValue,isMoving: false)
}