Browse Source

删除时,摄像头的时间以录制视频时长为准,其他以录音时长为准

harry 3 years ago
parent
commit
e61105f014

+ 18 - 10
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenCameraManager.swift

@@ -197,7 +197,7 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
             }
             //MARK: 进度计时器
 
-            let currDur = wself.videoModel.timelineCMIn + movieWrite.duration
+            let currDur = CMTime(seconds: wself.videoModel.timelineCMIn.seconds + movieWrite.duration.seconds, preferredTimescale: 1000)
             wself.currentAssetProgress = currDur
             wself.videoModel.timelineCMOut = currDur
             wself.recordProgressCallBack?(currDur)
@@ -290,7 +290,7 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
             let currDuration = videoS.1.timelineCMOut - videoS.1.timelineCMIn
             item.materialDuraion = item.materialDuraion - currDuration
             currentAssetProgress = videoS.1.timelineCMIn
-                        
+            BFLog(1, message: "删除时长:\(currDuration.seconds)")
             // 更新音频的时间点
             for model in item.voiceStickers {
                 if CMTimeCompare(currentAssetProgress, model.startCMTime) < 0 {
@@ -318,12 +318,18 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
     }
     
     // 录制失败 , 不足一秒时恢复上一次状态
-    func revertLast(){
-        if let vmod = recordItem?.videoStickers.last {
-            vmod.timelineCMOut = vmod.timelineCMIn
-            currentAssetProgress = vmod.timelineCMOut
+    func revertLast(_ modIndex : Int = -1){
+        var vmod : PQEditVisionTrackMaterialsModel?
+        if modIndex >= 0 && modIndex < recordItem?.videoStickers.count ?? 0{
+            vmod = recordItem?.videoStickers[modIndex]
+        }else{
+            vmod = recordItem?.videoStickers.last
+        }
+        if vmod != nil {
+            vmod!.timelineCMOut = vmod!.timelineCMIn
+            currentAssetProgress = vmod!.timelineCMOut
             recordItem?.thumbImgs.removeAll(where: { img in
-                vmod.thumImgs?.contains(img) ?? false
+                vmod!.thumImgs?.contains(img) ?? false
             })
             DispatchQueue.main.async {[weak self] in
                 guard let wself = self else { return }
@@ -331,7 +337,9 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
                 wself.cameraProgressV?.collectionV.reloadData()
             }
 //            vmod.thumImgs?.removeAll()
-            recordItem?.videoStickers.removeLast()
+            recordItem?.videoStickers.removeAll(where: { mod in
+                mod == vmod
+            })
         }
         
     }
@@ -368,7 +376,7 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
     func playRecordVoice(needPlay:Bool = true){
         // 播放音频
         if let mod = recordItem?.voiceStickers.first(where: { m in
-            CMTimeCompare(m.startCMTime, currentAssetProgress + CMTime(seconds: 0.033, preferredTimescale: 1000)) <= 0 && CMTimeCompare(m.endCMTime, currentAssetProgress) > 0
+            CMTimeCompare(m.startCMTime, currentAssetProgress + CMTime(seconds: 0.033, preferredTimescale: 1000)) <= 0 && CMTimeCompare(m.endCMTime, (currentAssetProgress + CMTime(seconds: 0.033, preferredTimescale: 1000))) > 0
         }){
             recordPlayer?.replaceCurrentItem(with: AVPlayerItem(url: URL(fileURLWithPath: mod.wavFilePath)))
             let tt = currentAssetProgress - mod.startCMTime
@@ -385,7 +393,7 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
         if let mod = recordItem?.videoStickers.sorted(by: { m1, m2 in
             m1.timelineCMIn.seconds < m2.timelineCMIn.seconds
         }).first(where: { mod in
-            CMTimeCompare(mod.timelineCMIn, currentAssetProgress + CMTime(seconds: 0.033, preferredTimescale: 1000)) <= 0 && CMTimeCompare(currentAssetProgress, mod.timelineCMOut) < 0
+            CMTimeCompare(mod.timelineCMIn, currentAssetProgress + CMTime(seconds: 0.033, preferredTimescale: 1000)) <= 0 && CMTimeCompare((currentAssetProgress + CMTime(seconds: 0.033, preferredTimescale: 1000)), mod.timelineCMOut) < 0
         }){
             currPlayTime = mod.timelineCMIn
             locationTo(time: mod.timelineCMIn)

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

@@ -901,7 +901,7 @@ public class BFRecordScreenController: BFBaseViewController {
                     }
                 }
 
-                BFLog(1, message: "添加录音文件:\(model.startCMTime.seconds) -- \(model.endCMTime.seconds)")
+                BFLog(1, message: "录音文件时长:\(model.startCMTime.seconds) -- \(model.endCMTime.seconds)")
                 wself.itemModels[wself.currItemModelIndex].voiceStickers.append(model)
                 // 录制结束回调
                 wself.recordEndHandle?(model)
@@ -1421,7 +1421,7 @@ public class BFRecordScreenController: BFBaseViewController {
                         itemModels[currItemModelIndex].materialDuraion = model.startCMTime
                     }else if currMediaType == .Camera {
                         let video = rscmanager.recordItem?.videoStickers.last
-                        rscmanager.revertLast()
+                        rscmanager.revertLast(modelIndex)
                         itemModels[currItemModelIndex].materialDuraion = video?.timelineCMIn ?? .zero
                     }
                 }
@@ -1435,68 +1435,77 @@ public class BFRecordScreenController: BFBaseViewController {
                             CMTimeCompare(m1.startCMTime, m2.startCMTime) < 0
                         }
                         
-                        if let first = tuples.first, let end = tuples.last {
-                            let currDuration = end.endCMTime - first.startCMTime
-                            itemModels[currItemModelIndex].materialDuraion = itemModels[currItemModelIndex].materialDuraion + currDuration
+                        var firstTime = 0.0
+                        var currDuration : CMTime = .zero
+                        
+                        if currMediaType == .Camera, let first = action.deletedCameras?.first, let end = action.deletedCameras?.last {
+                            firstTime = first.timelineCMIn.seconds
+                            currDuration = (end.timelineCMOut - first.timelineCMIn)
+                        }else if currMediaType == .Image, let first = tuples.first, let end = tuples.last {
+                            firstTime = first.startCMTime.seconds
+                            currDuration = end.endCMTime - first.startCMTime
+                        }
+                        
+                        BFLog(1, message: "恢复删除时长:\(currDuration.seconds)")
+                        
+                        itemModels[currItemModelIndex].materialDuraion = itemModels[currItemModelIndex].materialDuraion + currDuration
+                        
+                        // 恢复录音的时间点
+                        let list = itemModels[currItemModelIndex].voiceStickers.filter({ mod in
+                            CMTimeCompare(mod.startCMTime, CMTime(seconds: firstTime, preferredTimescale: 1000)) >= 0
+                        })
+                    
+                        list.forEach { mod in
+                            mod.startCMTime = mod.startCMTime + currDuration
+                            mod.endCMTime = mod.endCMTime + currDuration
                             
-                            // 恢复录音的时间点
-                            let list = itemModels[currItemModelIndex].voiceStickers.filter({ mod in
-                                CMTimeCompare(mod.startCMTime, first.startCMTime) >= 0
-                            })
+                            // 恢复字幕的时间点
+                            let titlsList = itemModels[currItemModelIndex].titleStickers.filter { tm in
+                                tm.recordId == mod.recordId
+                            }
+                            
+                            for titleM in titlsList {
+                                titleM.timelineIn = titleM.timelineIn + currDuration
+                                titleM.timelineOut = titleM.timelineOut + currDuration
+                            }
+                        }
                         
-                            list.forEach { mod in
-                                mod.startCMTime = mod.startCMTime + currDuration
-                                mod.endCMTime = mod.endCMTime + currDuration
-                                
-                                // 恢复字幕的时间点
-                                let titlsList = itemModels[currItemModelIndex].titleStickers.filter { tm in
-                                    tm.recordId == mod.recordId
-                                }
-                                
-                                for titleM in titlsList {
-                                    titleM.timelineIn = titleM.timelineIn + currDuration
-                                    titleM.timelineOut = titleM.timelineOut + currDuration
+                        if currMediaType == .Camera {
+                            rscmanager.cameraProgressV?.isHidden = false
+                            // 恢复录像的时间点
+                            _ = itemModels[currItemModelIndex].videoStickers.map { mod in
+                                if CMTimeCompare(mod.timelineCMIn, CMTime(seconds: firstTime, preferredTimescale: 1000)) >= 0{
+                                    mod.timelineCMIn = mod.timelineCMIn + currDuration
+                                    mod.timelineCMOut = mod.timelineCMOut + currDuration
                                 }
                             }
                             
-                            if currMediaType == .Camera {
-                                rscmanager.cameraProgressV?.isHidden = false
-                                // 恢复录像的时间点
-                                _ = itemModels[currItemModelIndex].videoStickers.map { mod in
-                                    if CMTimeCompare(mod.timelineCMIn, first.startCMTime) >= 0{
-                                        mod.timelineCMIn = mod.timelineCMIn + currDuration
-                                        mod.timelineCMOut = mod.timelineCMOut + currDuration
-                                    }
+                            // 摄像头录制的
+                            if let videoTuple = action.deletedCameras {
+                                itemModels[currItemModelIndex].videoStickers.append(contentsOf: videoTuple)
+                                itemModels[currItemModelIndex].videoStickers.sort { m1, m2 in
+                                    CMTimeCompare(m1.timelineCMIn, m2.timelineCMIn) < 0
                                 }
-                                
-                                // 摄像头录制的
-                                if let videoTuple = action.deletedCameras {
-                                    itemModels[currItemModelIndex].videoStickers.append(contentsOf: videoTuple)
-                                    itemModels[currItemModelIndex].videoStickers.sort { m1, m2 in
-                                        CMTimeCompare(m1.timelineCMIn, m2.timelineCMIn) < 0
-                                    }
-                                    itemModels[currItemModelIndex].thumbImgs.removeAll()
-                                    itemModels[currItemModelIndex].videoStickers.forEach { mod in
-                                        if let thumImag = mod.thumImgs{
-                                            itemModels[currItemModelIndex].thumbImgs.append(contentsOf: thumImag)
-                                        }
-                                    }
-                                    DispatchQueue.main.async {[weak self] in
-                                        guard let wself = self else { return }
-                                        
-                                        wself.rscmanager.cameraProgressV?.collectionV.reloadData()
+                                itemModels[currItemModelIndex].thumbImgs.removeAll()
+                                itemModels[currItemModelIndex].videoStickers.forEach { mod in
+                                    if let thumImag = mod.thumImgs{
+                                        itemModels[currItemModelIndex].thumbImgs.append(contentsOf: thumImag)
                                     }
                                 }
+                                DispatchQueue.main.async {[weak self] in
+                                    guard let wself = self else { return }
+                                    
+                                    wself.rscmanager.cameraProgressV?.collectionV.reloadData()
+                                }
                             }
                         }
+                    
                     }
                     
                     itemModels[currItemModelIndex].voiceStickers.append(contentsOf: tuples)
                     itemModels[currItemModelIndex].voiceStickers.sort { m1, m2 in
                         CMTimeCompare(m1.startCMTime, m2.startCMTime) < 0
                     }
-                    
-                        
                 }
                 // 恢复字幕