3 次代码提交 b47636e4fd ... 5bbe7b3231

作者 SHA1 备注 提交日期
  jsonwang 5bbe7b3231 1,显示下载中动画 2, 添加设置音乐起点 VIEW 3 年之前
  jsonwang cd46495703 添加下载音乐UI 显示 3 年之前
  jsonwang 874eb315cc 设置回调方法 3 年之前

+ 36 - 22
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenController.swift

@@ -416,6 +416,17 @@ public class BFRecordScreenController: BFBaseViewController {
         return toolV
 
     }()
+    
+    lazy var musicCutView: BFMusicCutView = {
+        let musicCutView = BFMusicCutView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
+        musicCutView.isHidden = true
+        musicCutView.cutTimeHandle = {[weak self] startTime  ,endTime ,musicInfo in
+            guard let wself = self else { return }
+            wself.hadChoosed(music:musicInfo)
+        }
+        return musicCutView
+    }()
+
 
     // 头像  add by ak
     lazy var avatarView: BFRecordAvatarView = {
@@ -782,22 +793,12 @@ public class BFRecordScreenController: BFBaseViewController {
     override public func viewDidLoad() {
         super.viewDidLoad()
         _ = disablePopGesture()
-<<<<<<< HEAD
-        
-        let path = Bundle.main.path(forResource: "11111", ofType: "mp3")
-        bgmModel = PQVoiceModel.init()
-        bgmModel?.wavFilePath = path
-        bgmModel?.startCMTime  = CMTime.init(value: 20 * 1000_000, timescale: 1000_000)
-        bgmModel?.endCMTime  = CMTime.init(value: 240 * 1000_000, timescale: 1000_000)
-        bgmModel?.volume = 100
-
-=======
+ 
    
-        let doubleTapGes = UITapGestureRecognizer(target: self, action: #selector(doubleTapAction(tap:)))
-        doubleTapGes.numberOfTapsRequired = 2
-        doubleTapGes.numberOfTouchesRequired = 1
-        view.addGestureRecognizer(doubleTapGes)
->>>>>>> c2402d0b7d3837cbda505e6074eded08f1528888
+//        let doubleTapGes = UITapGestureRecognizer(target: self, action: #selector(doubleTapAction(tap:)))
+//        doubleTapGes.numberOfTapsRequired = 2
+//        doubleTapGes.numberOfTouchesRequired = 1
+//        view.addGestureRecognizer(doubleTapGes)
         
         // 进入活跃状态
         PQNotification.addObserver(self, selector: #selector(didBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
@@ -988,7 +989,7 @@ public class BFRecordScreenController: BFBaseViewController {
                     //显示 UI
                     wself.voiceIconView.setNetImage(url: "\(wself.mSelectVoiced?.avatarUrl ?? "")")
                     if(!wself.voiceChangeStickerFinish(itemIndex: wself.currItemModelIndex)){
-
+                        wself.loadingView.isDownloadMusic = false
                         wself.loadingView.loadShow()
                     }
                     //静默转换语音
@@ -1030,6 +1031,8 @@ public class BFRecordScreenController: BFBaseViewController {
             }
             
         }
+        
+        view.addSubview(musicCutView);
          
         UIApplication.shared.keyWindow?.addSubview(loadingView)
 }
@@ -1447,15 +1450,16 @@ public class BFRecordScreenController: BFBaseViewController {
     // MARK: - 按钮事件响应
     
     func showCutView(_ model:PQVoiceModel) {
-        let v = BFMusicCutView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
-        view.addSubview(v)
-        v.bgmData = model
-//        DispatchQueue.main.async {
+
+        DispatchQueue.main.async { [weak self] in
+            guard let wself = self else { return }
+            wself.musicCutView.isHidden = false
+            wself.musicCutView.bgmData = model
 //            UIView.animate(withDuration: 0.2) {
 //                // 显示裁剪视图
 //                v.frame = CGRect(x: 0, y: cScreenHeigth - 220, width: cScreenWidth, height: 100)
 //            }
-//        }
+        }
     }
 
     override public func backBtnClick() {
@@ -2651,10 +2655,19 @@ public class BFRecordScreenController: BFBaseViewController {
     }
     
     func hadDowonloadMusic(model: PQVoiceModel) {
+        loadingView.isDownloadMusic = true
+        loadingView.loadShow()
         if let path = model.musicPath {
-            PQDownloadManager.downLoadFile(url: path) { musicUrl, err in
+            PQDownloadManager.downLoadFile(url: path) { [weak self ]musicUrl, err in
+                guard let wself = self else { return }
+                
                 if err == nil, musicUrl != nil {
                     model.wavFilePath = musicUrl
+                    wself.loadingView.removeLoading()
+                }else{
+                    cShowHUB(superView: wself.view, msg: "音乐下载失败")
+                    wself.bgmModel = nil
+                    wself.addMusicBtn.setTitle("选择音乐", for: .normal)
                 }
             }
         }
@@ -3148,6 +3161,7 @@ extension BFRecordScreenController: BFTTSManagerDelegte{
         
         if(isShowLoadView && !isFinish){
             loadingView.loadShow()
+            loadingView.isDownloadMusic = false
         }
      
         return isFinish

+ 10 - 1
BFRecordScreenKit/Classes/RecordScreen/View/BFLoadingView.swift

@@ -18,6 +18,15 @@ class BFLoadingView: UIView {
         loadingImage.image = imageInRecordScreenKit(by: "stuckPoint_edit_loading")
         return loadingImage
     }()
+    
+    //是否是下载音乐的模式,不发出回调不显示进度
+    public var isDownloadMusic:Bool = false{
+        didSet {
+            if isDownloadMusic {
+                titleL.text = ""
+            }
+        }
+    }
 
     lazy var closedBtn: UIButton = {
         let closedBtn = UIButton(type: .custom)
@@ -90,7 +99,7 @@ class BFLoadingView: UIView {
     // 隐藏动画
     @objc public func loadHidden() {
         removeLoading()
-        if cancelHandle != nil {
+        if cancelHandle != nil,!isDownloadMusic {
             cancelHandle!()
         }
         

+ 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)
 //        }
     }