Pārlūkot izejas kodu

设置回调方法

jsonwang 3 gadi atpakaļ
vecāks
revīzija
874eb315cc

+ 50 - 55
BFRecordScreenKit/Classes/RecordScreen/View/BFMusicCutView.swift

@@ -35,9 +35,10 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
             BFLog(message: "AVPlayerItemDidPlayToEndTime = \(notify)")
             guard let wself = self else { return }
             
-            avPlayer.seek(to: wself.startCMTime)
-            avPlayer.play()
-            wself.configPlayProgress(currentTime: wself.startCMTime.seconds)
+            wself.avPlayer.seek(to: CMTime.init(value: CMTimeValue((wself.bgmData?.currentTime ?? 0.0)) * Int64(playerTimescale), timescale: CMTimeScale(playerTimescale)))
+            wself.resetWave()
+
+            wself.avPlayer.play()
         }
         PQNotification.addObserver(forName: .AVPlayerItemNewErrorLogEntry, object: avPlayer.currentItem, queue: .main) { notify in
             BFLog(message: "AVPlayerItemNewErrorLogEntry = \(notify)")
@@ -54,20 +55,9 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
             let currentTime = CMTimeGetSeconds(avPlayer.currentItem?.currentTime() ?? CMTime.zero)
             wself.configPlayProgress(currentTime: currentTime)
         }
-        avPlayer.volume = 1.0
         return avPlayer
     }()
-
-    
-    // 总时长
-    lazy var totalTimeLab: UILabel = {
-        let totalTimeLab = UILabel()
-        totalTimeLab.backgroundColor = .red
-        totalTimeLab.textColor = UIColor.white
-        totalTimeLab.font = UIFont.systemFont(ofSize: 13)
-        totalTimeLab.text = "00:00 / 00:00"
-        return totalTimeLab
-    }()
+ 
     
     //放指针的背景图
     lazy var panCutBackView: UIView = {
@@ -106,16 +96,15 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
     // 操作板背景
     let backView = UIButton()
  
-    // 操作回调  isSame 是否点击的是同一个人,进行状态切换使用
-    public var MusiceCutCallBack: ((_ actionType: MusiceCutActionType, _ selectVoice: PQVoiceModel?,_ isSame:Bool ) -> Void)?
- 
     // 当前选择的音乐
     public var bgmData: PQVoiceModel? {
         didSet {
-            configPlayProgress(currentTime: bgmData?.currentTime ?? 0)
+            BFLog(message: "设置的音乐开始时间currentTime:\(bgmData?.currentTime ?? 0) 总时长\(bgmData?.duration ?? "")")
+            resetWave()
             //默认进来播放音乐
             playBGM()
-            currentProgressLab.center.x = panCutBackView.center.x
+
+            addCutViewLayout()
         }
     }
     
@@ -157,8 +146,9 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
     func addCutViewLayout() {
         let totalWidth = (frame.width - normalMargin * 2 - 6)
         let itemWidth = totalWidth / CGFloat(Double("\(bgmData?.duration ?? "1")") ?? 0.0)
-        panCutBackView.frame = CGRect(x: (normalMargin - cDefaultMargin * 1.5) + CGFloat(bgmData?.startTime ?? 0) * itemWidth, y: currentProgressLab.frame.maxY + 2, width: cDefaultMargin * 3, height: cDefaultMargin * 6)
+        panCutBackView.frame = CGRect(x: (normalMargin - cDefaultMargin * 1.5) + CGFloat(bgmData?.currentTime ?? 0) * itemWidth, y: currentProgressLab.frame.maxY + 2, width: cDefaultMargin * 3, height: cDefaultMargin * 6)
         cutRemindView.frame = CGRect(x: (panCutBackView.frame.width - 2) / 2, y: 0, width: 2, height: panCutBackView.frame.height)
+        currentProgressLab.centerX = panCutBackView.centerX
     }
     
     func playBGM(){
@@ -166,7 +156,8 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
         if(bgmData?.musicPath ?? "").count > 0{
             avPlayer.pause()
             avPlayer.replaceCurrentItem(with: AVPlayerItem(url: URL(string: (bgmData?.musicPath ?? ""))!))
-            avPlayer.seek(to: (bgmData?.startCMTime ?? .zero))
+            avPlayer.seek(to: CMTime.init(value: CMTimeValue((bgmData?.currentTime ?? 0.0) * playerTimescale), timescale: CMTimeScale(playerTimescale)))
+            
             avPlayer.play()
         }
     
@@ -229,7 +220,6 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
             make.top.equalToSuperview().offset(18)
         }
         
-        backView.addSubview(totalTimeLab)
         backView.addSubview(panCutBackView)
         panCutBackView.addSubview(cutRemindView)
         backView.addSubview(currentProgressLab)
@@ -289,7 +279,7 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
             lineLayer.fillColor = UIColor.black.cgColor
 
             waveLayers.append(lineLayer)
-            progressImage.layer.insertSublayer(lineLayer, at: 0)
+            progressImage.layer.addSublayer(lineLayer)
         }
     }
     
@@ -316,13 +306,10 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
             let endTime = Float64(Double("\(bgmData?.duration ?? "0")") ?? 0.0)
             BFLog(message: "裁剪背景音乐 duration = \(bgmData?.duration ?? "0"),startTime = \(startTime),endTime = \(endTime)")
             bgmData?.currentTime = startTime
-            bgmData?.startTime = startTime
-            bgmData?.endTime = endTime
-//            if cutTimeHandle != nil {
-//                cutTimeHandle!(startTime, endTime, bgmData)
-//            }
+
             
             avPlayer.seek(to: CMTime(value: CMTimeValue(startTime * playerTimescale), timescale: CMTimeScale(playerTimescale)))
+            avPlayer.play()
             // 重置波纹
             resetWave()
         }
@@ -330,46 +317,49 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
     /// 重置波纹
     /// - Returns: <#description#>
     func resetWave() {
-        let startTotal = (Float64(waveLayers.count) / (Float64("\(bgmData?.duration ?? "0")") ?? 1)) * Float64(bgmData?.startTime ?? 0)
+        let startTotal = (Float64(waveLayers.count) / (Float64("\(bgmData?.duration ?? "0")") ?? 1)) * Float64(bgmData?.currentTime ?? 0)
         if startTotal.isNaN {
             return
         }
         let totalCount = progressImage.layer.sublayers?.count ?? 0
         if totalCount > 0 {
             for index in 0..<Int(totalCount) {
-                if (totalCount - index - 1) < totalCount {
-                    if index < Int(startTotal) {
-                        (progressImage.layer.sublayers?[totalCount - index - 1] as? CAShapeLayer)?.strokeColor = UIColor.hexColor(hexadecimal: "#202020").cgColor
-                    } else {
-                        (progressImage.layer.sublayers?[totalCount - index - 1] as? CAShapeLayer)?.strokeColor = UIColor.white.cgColor
-                    }
-                    (progressImage.layer.sublayers?[totalCount - index - 1] as? CAShapeLayer)?.setNeedsDisplay()
+
+                if index < Int(startTotal) {
+                    (progressImage.layer.sublayers?[index] as? CAShapeLayer)?.strokeColor = UIColor.hexColor(hexadecimal: "#202020").cgColor
+                } else {
+
+                    
+                    (progressImage.layer.sublayers?[index] as? CAShapeLayer)?.strokeColor = UIColor.white.cgColor
+                
                 }
+                (progressImage.layer.sublayers?[totalCount - index - 1] as? CAShapeLayer)?.setNeedsDisplay()
+
             }
         }
     }
     func configPlayProgress(currentTime: Float64) {
-        bgmData?.currentTime = currentTime < 0 ? 0 : currentTime
         let totalCount = progressImage.layer.sublayers?.count ?? 0
-        // 循环播放时重置
-        if (bgmData?.currentTime ?? 0) == (bgmData?.startTime ?? 0) {
-            // 重置波纹
-            resetWave()
-        }
-        if Float64(bgmData?.currentTime ?? 0) > Float64(bgmData?.startTime ?? 0) {
-            let sdd = (Float64(bgmData?.currentTime ?? 0) - Float64(bgmData?.startTime ?? 0))
-            let total = (Float64(waveLayers.count) / (Float64("\(bgmData?.duration ?? "0")") ?? 1)) * sdd
-            let startTotal = (Float64(waveLayers.count) / (Float64("\(bgmData?.duration ?? "0")") ?? 1)) * Float64(bgmData?.startTime ?? 0)
-
+        if currentTime >= 0  {
+            //播放的开始点
+            let startPoint = (Float64(waveLayers.count) / (Float64("\(bgmData?.duration ?? "0")") ?? 1)) * Float64(bgmData?.currentTime ?? 0)
+            //已经播放到的点
+            let endPoint = (Float64(waveLayers.count) / (Float64("\(bgmData?.duration ?? "0")") ?? 1)) * currentTime
+      
+            if(endPoint < startPoint){
+                BFLog(message: "startPoint: \(startPoint) endPoint:\(endPoint)")
+                return
+            }
             if totalCount > 0 {
-                for index in 0..<Int(total) {
-                    var tempIndex = (totalCount - index - 1 - Int(startTotal))
+                for index in Int(startPoint)...Int(endPoint) {
+                    var tempIndex = index
                     if tempIndex < 0 {
                         tempIndex = 0
                     }
                     if tempIndex >= totalCount - 1 {
                         tempIndex = totalCount - 1
                     }
+                    BFLog(message: "indesssssss \(tempIndex)")
                     (progressImage.layer.sublayers?[tempIndex] as? CAShapeLayer)?.strokeColor = UIColor.hexColor(hexadecimal: "#389AFF").cgColor
                     (progressImage.layer.sublayers?[tempIndex] as? CAShapeLayer)?.setNeedsDisplay()
                 }
@@ -398,15 +388,20 @@ class BFMusicCutView: UIView ,UIGestureRecognizerDelegate {
 
     @objc func okBtnAction() {
         hidden()
-//        if MusiceCutCallBack != nil {
-//            MusiceCutCallBack!(.MusiceCutActionConfirm, selectVoice,false)
-//        }
+
+        bgmData?.startCMTime = CMTime.init(value: CMTimeValue((bgmData?.currentTime ?? 0.0) * playerTimescale), timescale: CMTimeScale(playerTimescale))
+        
+        BFLog(message: "最后设置的开始时间是\(bgmData?.startCMTime.seconds ?? 0.0)")
+ 
+        if cutTimeHandle != nil,bgmData != nil {
+            cutTimeHandle!(0, 0, bgmData)
+        }
     }
 
     @objc func deleteBtnAction() {
    
-//        if MusiceCutCallBack != nil {
-//            MusiceCutCallBack!(.MusiceCutActionDelete, selectVoice,false)
+//        if cutTimeHandle != nil {
+//            cutTimeHandle!(.MusiceCutActionDelete, selectVoice,false)
 //        }
     }