Parcourir la source

视频缩略图进度展示

harry il y a 3 ans
Parent
commit
e531f01a9c

+ 32 - 4
BFRecordScreenKit/Classes/BFRecordScreenController.swift

@@ -207,6 +207,34 @@ public class BFRecordScreenController: BFBaseViewController {
         
     }()
     
+    lazy var progressThumV : BFVideoThumbProgressView = {
+        let vv = BFVideoThumbProgressView(frame: CGRect(x: 0, y: 20, width: cScreenWidth, height: 50), videoAsset: self.avasset!)
+        vv.dragScrollProgressHandle = { process in
+            DispatchQueue.main.async {[weak self] in
+                guard let sself = self else {
+                    return
+                }
+                if sself.isNormalPlaying || sself.isRecording {
+                    sself.pause()
+                    sself.isDragingProgressSlder = true
+                }
+                sself.changeProgress(progress: process)
+            }
+        }
+        vv.dragEndHandle = { [weak self] process in
+            
+            guard let sself = self else {
+                return
+            }
+            sself.changeProgress(progress: process)
+
+            sself.isDragingProgressSlder = false
+            sself.currentPlayRecordIndex = -1
+            sself.hadPrepareToPlayRecord = false
+        }
+        return vv
+    }()
+    
     //MARK: ------------------ 生命周期
     deinit {
         cleanMovieTarget()
@@ -588,8 +616,7 @@ public class BFRecordScreenController: BFBaseViewController {
                 if let urlass = asset as? AVURLAsset {
                     self?.avasset = urlass
                     DispatchQueue.main.async {[weak self] in
-                        let progressThumV = BFVideoThumbProgressView(frame: CGRect(x: 0, y: 20, width: cScreenWidth, height: 50), videoAsset: self!.avasset!)
-                        self?.bottomeView.addSubview(progressThumV)
+                        self?.bottomeView.addSubview(self!.progressThumV)
                     }
                 }
             })
@@ -634,6 +661,7 @@ public class BFRecordScreenController: BFBaseViewController {
                     DispatchQueue.main.async { [weak self] in
                         self?.progessSilde.value = Float(CMTimeGetSeconds(time) / CMTimeGetSeconds(item.duration))
                         self?.progreddL.text = String(format: "%.2f / %.2f", CMTimeGetSeconds(time), CMTimeGetSeconds(item.duration))
+                        self?.progressThumV.progress = time.seconds
                     }
                 }
             } as? NSKeyValueObservation
@@ -670,9 +698,9 @@ public class BFRecordScreenController: BFBaseViewController {
     func changeProgress(progress:Float) {
         if let duration = assetPlayer?.currentItem?.duration {
             self.currentAssetProgress = CMTime(value: CMTimeValue(progress * Float(CMTimeGetSeconds(duration)) * 100), timescale: 100)
-            assetPlayer!.seek(to: self.currentAssetProgress, toleranceBefore: CMTime(value: 1, timescale: 1000), toleranceAfter: CMTime(value: 1, timescale: 1000)) {[weak self] finished in
+            assetPlayer!.seek(to: self.currentAssetProgress, toleranceBefore: CMTime(value: 1, timescale: 1000), toleranceAfter: CMTime(value: 1, timescale: 1000)) { finished in
 //                if finished{
-//                    BFLog(1, message: "拖动成功")
+//                    BFLog(1, message: "拖动成功 \(progress)")
 //                    self?.movie?.startProcessing()
 //                }
             }

+ 10 - 2
BFRecordScreenKit/Classes/BFVideoThumbImageFetchHelper.swift

@@ -13,12 +13,20 @@ import BFCommonKit
 /// - parameter fileUrl                 : 视频地址
 /// - parameter fps                     : 自定义帧数 每秒内取的帧数
 /// - parameter splitCompleteClosure    : 回调
-func splitVideoFileUrlFps(urlAsset:AVURLAsset, fps:Float, splitCompleteClosure:@escaping ((Bool, [UIImage]?) -> Void)) {
+func splitVideoFileUrlFps(urlAsset:AVURLAsset, fps:Float, range:NSRange? = nil, splitCompleteClosure:@escaping ((Bool, [UIImage]?) -> Void)) {
     var splitImages = [UIImage]()
     
     var times = [NSValue]()
     
-    for i in 0...Int(urlAsset.duration.seconds * Float64(fps)) {
+    var start = 0
+    var end = Int(urlAsset.duration.seconds * Float64(fps))
+    
+    if range != nil {
+        start = range!.location
+        end = start + range!.length
+    }
+    
+    for i in start...end {
         let timeValue = NSValue(time: CMTimeMake(value: Int64(i), timescale: Int32(fps)) )
         
         times.append(timeValue)

+ 55 - 8
BFRecordScreenKit/Classes/BFVideoThumbProgressView.swift

@@ -13,6 +13,17 @@ import SnapKit
 
 class BFVideoThumbProgressView: UIView {
     var videoAsset : AVURLAsset?
+    var dragScrollProgressHandle : ((Float) -> Void)?
+    var dragEndHandle : ((Float) -> Void)?
+    var isDrag = false
+    var progress:Double = 0 {
+        didSet{
+            if let second = self.videoAsset?.duration.seconds, second > 0 {
+                let w = progressView.contentSize.width - width
+                progressView.contentOffset = CGPoint(x: progress * w / second, y: 0)
+            }
+        }
+    }
     
     var thumbImgs = [UIImage]()
     
@@ -20,7 +31,17 @@ class BFVideoThumbProgressView: UIView {
         super.init(frame: frame)
         self.videoAsset = videoAsset
         addSubview(progressView)
-
+        
+        let line = UIView()
+        line.backgroundColor = .white
+        line.layer.shadowColor = UIColor.black.cgColor
+        line.layer.shadowOffset = CGSize(width: 1, height: 1)
+        addSubview(line)
+        line.snp.makeConstraints { make in
+            make.width.equalTo(2)
+            make.center.height.equalToSuperview()
+        }
+        
         splitVideoFileUrlFps(urlAsset: videoAsset, fps: 2) {[weak self] isSuccess, images in
             if isSuccess{
                 self?.thumbImgs = images!
@@ -29,6 +50,7 @@ class BFVideoThumbProgressView: UIView {
                         for (i, img) in images!.enumerated() {
                             let iv = UIImageView(image: img)
                             iv.contentMode = .scaleAspectFill
+                            iv.clipsToBounds = true
                             sself.progressView.addSubview(iv)
                             iv.snp.makeConstraints { make in
                                 make.left.equalTo(CGFloat(i) * sself.height + sself.width * 0.5)
@@ -53,17 +75,11 @@ class BFVideoThumbProgressView: UIView {
         sv.backgroundColor = .clear
         sv.decelerationRate = .fast
         sv.showsHorizontalScrollIndicator = false
-        
+        sv.delegate = self
         
         return sv
     }()
     
-    override func didMoveToWindow() {
-        super.didMoveToWindow()
-        
-        
-    }
-    
     override func layoutSubviews() {
         super.layoutSubviews()
         progressView.snp.makeConstraints { make in
@@ -72,3 +88,34 @@ class BFVideoThumbProgressView: UIView {
         
     }
 }
+
+extension BFVideoThumbProgressView : UIScrollViewDelegate {
+    func scrollViewDidScroll(_ scrollView: UIScrollView) {
+        if isDrag{
+            let dur = scrollView.contentOffset.x / (scrollView.contentSize.width - self.width)
+            self.dragScrollProgressHandle?(Float(dur))
+        }
+    }
+    
+    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
+        isDrag = true
+        let dur = scrollView.contentOffset.x / (scrollView.contentSize.width - self.width)
+        self.dragScrollProgressHandle?(Float(dur))
+    }
+    
+    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
+        if !decelerate {
+            let dur = scrollView.contentOffset.x / (scrollView.contentSize.width - self.width)
+            isDrag = false
+            dragEndHandle?(Float(dur))
+        }
+    }
+    
+    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
+        let dur = scrollView.contentOffset.x / (scrollView.contentSize.width - self.width)
+        isDrag = false
+        dragEndHandle?(Float(dur))
+
+    }
+    
+}