Forráskód Böngészése

Merge branch 'master' of https://git.yishihui.com/iOS/BFRecordScreenKit

* 'master' of https://git.yishihui.com/iOS/BFRecordScreenKit:
  恢复字幕数据
  // BFVideoThumbProgressView
  add BFVideoThumbProgressView
harry 3 éve
szülő
commit
638b7366b3

+ 48 - 14
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenController.swift

@@ -146,6 +146,8 @@ public class BFRecordScreenController: BFBaseViewController {
     var touchStart: CGPoint = CGPoint(x: 0, y: 0)
     var avplayerTimeObserver: NSKeyValueObservation?
 
+    var indirectionView: BFIndirectionProgressView?
+    
     lazy var progreddL: UILabel = {
         let l = UILabel()
         l.textAlignment = .center
@@ -407,7 +409,7 @@ public class BFRecordScreenController: BFBaseViewController {
         // add by ak 取 nsl token
         BFRecordScreenViewModel.getNlsAccessToken {[weak self] token, appkey in
             BFLog(message: "nls appkey is \(appkey), token is \(token)")
-            self?.speechTranscriberUtil = PQSpeechTranscriberUtil(token, appid: appkey)
+//            self?.speechTranscriberUtil = PQSpeechTranscriberUtil(token, appid: appkey)
         }
 
         view.backgroundColor = .black
@@ -762,10 +764,18 @@ public class BFRecordScreenController: BFBaseViewController {
             assetPlayer?.play()
             // 暂停状态
             (collectionView.cellForItem(at: IndexPath(item: currItemModelIndex, section: 0)) as? BFImageCoverViewCell)?.playBtn.isSelected = true
-        } else {
-            recorderManager.audioRecorder?.recorderProgross = { [weak self] progress in
+        }
+        recorderManager.audioRecorder?.recorderProgross = { [weak self] progress in
+            if self?.itemModels[self?.currItemModelIndex ?? 0].mediaType == .IMAGE {
                 self?.imageRecordProgress(isRecord: true, progress: progress)
             }
+//            self?.indirectionView?.setProgress(index: (self?.itemModels[self?.currItemModelIndex ?? 0].voiceStickers.count ?? 0), start: self?.currentAssetProgress.seconds ?? 0, progress: progress)
+        }
+        recorderManager.audioRecorder?.recorderStartHandle = {[weak self] in
+//            if self?.indirectionView == nil {
+//                self?.indirectionView = BFIndirectionProgressView.init(frame: self?.progressThumV.progessIndicateBackV.bounds ?? CGRect.zero, percenWidth: self?.itemModels[self?.currItemModelIndex ?? 0].mediaType == .IMAGE ? (self?.progressThumV.thumbImageWidth ?? 0) / 2 : 0, totalDuration: self?.itemModels[self?.currItemModelIndex ?? 0].materialDuraion ?? 0)
+//                self?.progressThumV.progessIndicateBackV.addSubview((self?.indirectionView)!)
+//            }
         }
     }
 
@@ -814,7 +824,17 @@ public class BFRecordScreenController: BFBaseViewController {
                             itemModels[currItemModelIndex].voiceStickers.insert(tuple.0, at: tuple.1)
                         }
                     }
-
+                    
+                    //恢复字幕
+                    let titleTuples = action.deletedTittles
+                    if titleTuples != nil, titleTuples!.count > 0 {
+                       
+                        titleTuples?.forEach { titleTuple in
+                            itemModels[currItemModelIndex].titleStickers.insert(titleTuple.0, at: titleTuple.1)
+                        }
+                    }
+                    
+                    
                     drawOrUpdateRecordProgessLable()
                     jumpTime = model.startTime
                 }
@@ -829,6 +849,17 @@ public class BFRecordScreenController: BFBaseViewController {
                         itemModels[currItemModelIndex].voiceStickers.insert(tuple.0, at: tuple.1)
                     }
                 }
+                
+                //恢复字幕
+                let titleTuples = action.deletedTittles
+                if titleTuples != nil, titleTuples!.count > 0 {
+                   
+                    titleTuples?.forEach { titleTuple in
+                        itemModels[currItemModelIndex].titleStickers.insert(titleTuple.0, at: titleTuple.1)
+                    }
+                }
+                
+                
                 drawOrUpdateRecordProgessLable()
             } else {}
             events.removeLast()
@@ -1200,16 +1231,18 @@ public class BFRecordScreenController: BFBaseViewController {
     /// - Parameter CMTime: <#CMTime description#>
     func periodicTimeObserver(item: AVPlayerItem?, time: CMTime) {
         // 进度监控
-        currentAssetProgress = CMTime(seconds: time.seconds, preferredTimescale: 1000)
-        BFLog(1, message: "curr:\(CMTimeGetSeconds(currentAssetProgress))")
-        if CMTimeGetSeconds(item?.duration ?? CMTime.zero) > 0 {
-            DispatchQueue.main.async { [weak self] in
-                self?.progreddL.text = String(format: "%@", CMTimeGetSeconds(time).formatDurationToHMS())
-                let su = !(self?.isDragingProgressSlder ?? false) || (self?.isRecording ?? false && self?.isNormalPlaying ?? false)
-                if su {
-                    self?.progressThumV.progress = time.seconds
+        if itemModels[currItemModelIndex].mediaType == .VIDEO {
+            currentAssetProgress = CMTime(seconds: time.seconds, preferredTimescale: 1000)
+            BFLog(1, message: "curr:\(CMTimeGetSeconds(currentAssetProgress))")
+            if CMTimeGetSeconds(item?.duration ?? CMTime.zero) > 0 {
+                DispatchQueue.main.async { [weak self] in
+                    self?.progreddL.text = String(format: "%@", CMTimeGetSeconds(time).formatDurationToHMS())
+                    let su = !(self?.isDragingProgressSlder ?? false) || (self?.isRecording ?? false && self?.isNormalPlaying ?? false)
+                    if su {
+                        self?.progressThumV.progress = time.seconds
+                    }
+                    self?.updateSubtitle(time: time)
                 }
-                self?.updateSubtitle(time: time)
             }
         }
     }
@@ -1254,6 +1287,7 @@ public class BFRecordScreenController: BFBaseViewController {
             guard let sself = self else {
                 return
             }
+            
             sself.progressThumV.progessIndicateBackV.subviews.forEach { vv in
                 vv.removeFromSuperview()
             }
@@ -1414,7 +1448,7 @@ public extension BFRecordScreenController {
             isNormalPlaying = true
             playRecord(at: currentAssetProgress, periodicTimeObserver: { [weak self] currentT, currentItem in
                 BFLog(message: "播放一段进度:\(currentT),\(currentItem)")
-                self?.imageRecordProgress(progress: CMTimeGetSeconds(currentT))
+                self?.imageRecordProgress(progress: CMTimeGetSeconds(currentT) / 2)
             }, didPlayToEndTime: { [weak self] recordedAudio, currentItem in
                 BFLog(message: "播放一段结束:\(String(describing: recordedAudio)),\(String(describing: currentItem))")
                 if (recordedAudio?.endTime ?? 0) >= (self?.itemModels[self?.currItemModelIndex ?? 0].voiceStickers.last?.endTime ?? 0) {

+ 88 - 0
BFRecordScreenKit/Classes/RecordScreen/View/BFIndirectionProgressView.swift

@@ -0,0 +1,88 @@
+//
+//  BFIndirectionProgressView.swift
+//  BFRecordScreenKit
+//
+//  Created by SanW on 2021/12/18.
+//  Copyright © 2021 BytesFlow. All rights reserved.
+//
+
+import BFCommonKit
+import BFMediaKit
+import UIKit
+
+class BFIndirectionProgressView: UIView {
+    var indirecColor: UIColor = UIColor.clear
+    var themeColor: UIColor = UIColor.hexColor(hexadecimal: "#28BE67")
+    var progressHeight: CGFloat = 6
+    var percenWidth: CGFloat = 0
+    var totalDuration: Float64 = 0
+
+    override private init(frame: CGRect) {
+        super.init(frame: frame)
+    }
+
+    init(frame: CGRect, indirecColor: UIColor = UIColor.clear, themeColor: UIColor = UIColor.hexColor(hexadecimal: "#28BE67"), percenWidth: CGFloat = 0, progressHeight: CGFloat = 6, totalDuration: Float64 = 0) {
+        super.init(frame: frame)
+        self.indirecColor = indirecColor
+        self.themeColor = themeColor
+        self.progressHeight = progressHeight
+        self.percenWidth = percenWidth
+        if self.percenWidth <= 0, totalDuration > 0 {
+            self.percenWidth = frame.width / totalDuration
+        }
+    }
+
+    required init?(coder _: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+
+    func updateProgressViews(items: [PQVoiceModel]) {
+        subviews.forEach { vv in
+            vv.removeFromSuperview()
+        }
+        items.forEach { model in
+            createItemView(minX: model.startTime * percenWidth, width: (model.endTime - model.startTime) * percenWidth)
+        }
+    }
+
+    func setProgress(index: Int, start: CGFloat = 0, progress: Float64) {
+        if subviews.count > index {
+            BFLog(message: "设置进度-\(index),\(progress),\(progress * percenWidth)")
+            subviews[index].frame.size.width = progress * percenWidth
+        } else {
+            BFLog(message: "设置进度-添加一个录音:\(index),\(progress),\(start * percenWidth)")
+            createItemView(minX: start * percenWidth)
+        }
+        
+    }
+
+    func currentItem(start: CGFloat = 0, progress: Float64) -> UIView {
+        let newRange = CMTimeRange(start: CMTime(seconds: start, preferredTimescale: 1000), end: CMTime(seconds: start + progress, preferredTimescale: 1000))
+        var currentIndex: Int?
+        for (index,item) in subviews.enumerated() {
+            let originRange = CMTimeRange(start: CMTime(seconds: item.frame.minX / percenWidth, preferredTimescale: 1000), end: CMTime(seconds: item.frame.width / percenWidth, preferredTimescale: 1000))
+            if CMTimeRangeGetIntersection(originRange, otherRange: newRange).duration.seconds > 0 {
+                currentIndex = index
+//                if
+                break
+            }
+        }
+        if currentIndex != nil {
+            return subviews[currentIndex!]
+        } else {
+           return createItemView(minX: start * percenWidth)
+        }
+    }
+    
+    func deleteItem(index: Int) {
+        subviews[index].removeFromSuperview()
+    }
+
+    func createItemView(minX: CGFloat, width: CGFloat = 0, indirec: Bool = false)  -> UIView{
+        let lineV = UIView(frame: CGRect(x: minX, y: 0, width: width, height: progressHeight))
+        lineV.backgroundColor = indirec ? indirecColor : themeColor
+        lineV.tag = indirec ? 1 : 2
+        addSubview(lineV)
+        return lineV
+    }
+}

+ 6 - 2
BFRecordScreenKit/Classes/RecordScreen/View/BFVideoThumbProgressView.swift

@@ -152,7 +152,7 @@ class BFVideoThumbProgressView: UIView {
     }
 
     func appendThumb(progress: Double = 0) {
-        let count: Int = Int((progress < 1 ? progress * 100 : progress) / 2)
+        let count: Int = Int(progress / 2)
         if recordItem?.mediaType == .IMAGE, thumbImgs.count < (count - 5) {
             guard let image = recordItem?.coverImg else {
                 return
@@ -182,6 +182,9 @@ class BFVideoThumbProgressView: UIView {
     /// 更新进度
     /// - Parameter progress: <#progress description#>
     func updateProgress(progress: Double = 0) {
+        if  progressView.contentSize.width <= 0 {
+            return
+        }
         if recordItem?.mediaType == .VIDEO {
             if let second = recordItem?.videoAsset?.duration.seconds, second > 0 {
                 let w = progressView.contentSize.width - width
@@ -189,7 +192,8 @@ class BFVideoThumbProgressView: UIView {
             }
         } else if recordItem?.mediaType == .IMAGE {
 //            if (recordItem?.materialDuraion ?? 0) > progress {
-            progressView.setContentOffset(CGPoint(x: (progress < 1 ? progress * 100 : progress) * thumbImageWidth / 2.0, y: 0), animated: true)
+            BFLog(message: "updateProgress:\(progress)")
+            progressView.contentOffset = CGPoint(x: progress * thumbImageWidth / 2.0, y: 0)
             appendThumb(progress: progress)
 //            }
         }