浏览代码

1.修改不够一秒的逻辑

wenweiwei 3 年之前
父节点
当前提交
35f8df2705

+ 16 - 23
BFRecordScreenKit/Classes/BFVoiceRecordManager.swift

@@ -97,13 +97,7 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate {
         if duration > 1 {
             if mIsCancel {
                 // 删除录制的原文件
-                if FileManager.default.fileExists(atPath: outfile) {
-                    do {
-                        try FileManager.default.removeItem(at: NSURL.fileURL(withPath: outfile))
-                    } catch {
-                        BFLog(message: "删除文件出错 == \(error) \(outfile)")
-                    }
-                }
+                deleteFile(outfile: outfile)
                 cancelRecordHandle?(nil)
             } else {
                 var beginRecordTime1 = Date()
@@ -113,14 +107,7 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate {
                 BFPcmToWaveTool().pcmToWav(inFileName: outfile, outFileName: wavFilePath)
                 BFLog(message: "转 WAV用时\(Date().timeIntervalSince(beginRecordTime1))")
                 // 删除录制的原文件
-                if FileManager.default.fileExists(atPath: outfile) {
-                    do {
-                        try FileManager.default.removeItem(at: NSURL.fileURL(withPath: outfile))
-                    } catch {
-                        BFLog(message: "删除文件出错 == \(error) \(outfile)")
-                    }
-                }
-
+                deleteFile(outfile: outfile)
                 // 2处理降噪
                 beginRecordTime1 = Date()
                 let noiseFilePath = wavFilePath.replacingOccurrences(of: ".wav", with: "_noise.wav")
@@ -134,17 +121,10 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate {
                     BFLog(message: "降噪用时\(Date().timeIntervalSince(beginRecordTime1))")
                 }
                 // 删除临时 wav 文件
-                if FileManager.default.fileExists(atPath: wavFilePath) {
-                    do {
-                        try FileManager.default.removeItem(at: NSURL.fileURL(withPath: wavFilePath))
-                    } catch {
-                        BFLog(message: "删除文件出错 == \(error) \(wavFilePath)")
-                    }
-                }
+                deleteFile(outfile: wavFilePath)
             }
 
         } else {
-            cShowHUB(superView: nil, msg: "最短录制1秒")
             cancelRecordHandle?(voiceModel)
         }
 
@@ -152,6 +132,19 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate {
         isStoping = false
     }
 
+    
+    /// 删除文件
+    /// - Parameter outfile: <#outfile description#>
+    public func deleteFile(outfile:String) {
+        if FileManager.default.fileExists(atPath: outfile) {
+            do {
+                try FileManager.default.removeItem(at: NSURL.fileURL(withPath: outfile))
+            } catch {
+                BFLog(message: "删除文件出错 == \(error) \(outfile)")
+            }
+        }
+    }
+    
     public func eventCallback(_: BFRecorderManager, asrResult: String, audioFilePath: String) {
         // 最后输出的文件是降噪后的
         let noiseFilePath = audioFilePath.replacingOccurrences(of: ".pcm", with: "_noise.wav")

+ 37 - 21
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenController.swift

@@ -516,6 +516,12 @@ public class BFRecordScreenController: BFBaseViewController {
                 // 加入到语音数组里
                 model.endCMTime = sself.currentAssetProgress
                 BFLog(1, message: "录制结束当前录音文件:\(model.wavFilePath ?? "")-\(model.startCMTime.seconds)-\(model.endCMTime.seconds)-\(model.endCMTime.seconds - model.startCMTime.seconds)")
+                /// 注:录音机回调的录音时长大于一秒,而业务逻辑计算的会小于一秒
+                if (model.endCMTime.seconds - model.startCMTime.seconds) < 1 {
+                    // 取消录制
+                    sself.recordManagerCancelRecord(voiceModel: model)
+                    return
+                }
                 // ********** 开始处理冲突的录制部分
                 let newRange = CMTimeRange(start: model.startCMTime, end: model.endCMTime)
 
@@ -596,26 +602,8 @@ public class BFRecordScreenController: BFBaseViewController {
             }
         }
         recorderManager?.cancelRecordHandle = { [weak self] voiceModel in
-            // add by ak 取消录制后删除对应字幕数据,这里可恢复操作吗?
-            var subtitleCount = self?.itemModels[self?.currItemModelIndex ?? 0].titleStickers.count ?? 0
-            BFLog(2, message: "删除\(voiceModel?.wavFilePath ?? "")对应的字幕 前 count\(subtitleCount)")
-            if subtitleCount > 0 {
-                for title in self!.itemModels[self?.currItemModelIndex ?? 0].titleStickers {
-                    if title.audioFilePath == voiceModel?.wavFilePath ?? "" {
-                        if let index = self?.itemModels[self?.currItemModelIndex ?? 0].titleStickers.firstIndex(of: title) {
-                            self?.itemModels[self?.currItemModelIndex ?? 0].titleStickers.remove(at: index)
-                        }
-                    }
-                }
-            }
-            subtitleCount = self?.itemModels[self?.currItemModelIndex ?? 0].titleStickers.count ?? 0
-            BFLog(2, message: "删除\(voiceModel?.wavFilePath ?? "")对应的字幕  后 count\(subtitleCount)")
-            /// 重置进度
-            self?.currentAssetProgress = CMTime(seconds: voiceModel?.startCMTime.seconds ?? 0, preferredTimescale: 1000)
-            self?.resetCurrentProgress()
-            // 移除
-            self?.indirectionView?.deleteItem(isCurrent: true)
-            self?.recorderManager?.voiceModel = nil
+            // 取消录制
+            self?.recordManagerCancelRecord(voiceModel: voiceModel)
         }
 
         recorderManager?.NeoNuiDebugHandle = { [weak self] msg in
@@ -1059,10 +1047,38 @@ public class BFRecordScreenController: BFBaseViewController {
         recorderManager?.stopRecord(isCancel: true)
         progressThumV.progressView.isUserInteractionEnabled = true
         collectionView.isScrollEnabled = true
-
         pause()
     }
 
+    /// 不足一秒,主动取消录制
+    /// - Parameter voiceModel: <#voiceModel description#>
+    @objc func recordManagerCancelRecord(voiceModel: PQVoiceModel?) {
+        if currentAssetProgress.seconds - (recorderManager?.voiceModel?.startCMTime.seconds ?? 0) < 1.0 {
+            cShowHUB(superView: nil, msg: "最短录制1秒")
+        }
+        // 删除文件
+        recorderManager?.deleteFile(outfile: voiceModel?.wavFilePath)
+        var subtitleCount = itemModels[currItemModelIndex].titleStickers.count
+        BFLog(2, message: "删除\(voiceModel?.wavFilePath ?? "")对应的字幕 前 count\(subtitleCount)")
+        if subtitleCount > 0 {
+            for title in itemModels[currItemModelIndex].titleStickers {
+                if title.audioFilePath == voiceModel?.wavFilePath ?? "" {
+                    if let index = itemModels[currItemModelIndex].titleStickers.firstIndex(of: title) {
+                        itemModels[currItemModelIndex].titleStickers.remove(at: index)
+                    }
+                }
+            }
+        }
+        subtitleCount = itemModels[currItemModelIndex].titleStickers.count
+        BFLog(2, message: "删除\(voiceModel?.wavFilePath ?? "")对应的字幕  后 count\(subtitleCount)")
+        /// 重置进度
+        currentAssetProgress = CMTime(seconds: voiceModel?.startCMTime.seconds ?? 0, preferredTimescale: 1000)
+        resetCurrentProgress()
+        // 移除
+        indirectionView?.deleteItem(isCurrent: true)
+        recorderManager?.voiceModel = nil
+    }
+
     // 撤销
     @objc func withdrawAction() {
         pause()

+ 5 - 2
BFRecordScreenKit/Classes/RecordScreen/View/BFIndirectionProgressView.swift

@@ -82,9 +82,10 @@ class BFIndirectionProgressView: UIView {
         let vc = subviews.first { [weak self] sub in
             (self?.currentItem?.frame.minX ?? 0) < sub.frame.minX && sub.frame.minX < (self?.currentItem?.frame.maxX ?? 0)
         }
-        if vc !== nil {
+        if vc != nil {
             deletedView = vc
             vc?.removeFromSuperview()
+            BFLog(message: "添加覆盖view-添加:deletedView = \(String(describing: deletedView))")
         }
     }
 
@@ -133,18 +134,20 @@ class BFIndirectionProgressView: UIView {
     /// 删除某个view
     /// - Parameter index: <#index description#>
     func deleteItem(index: Int = 0, isCurrent: Bool = false, isImage _: Bool = false) {
+        BFLog(message: "添加覆盖view-不足一秒:\(isCurrent),deletedView = \(String(describing: deletedView))")
         if isCurrent {
             currentItem?.removeFromSuperview()
             currentItem = nil
             if deletedView != nil {
+                BFLog(message: "添加view")
                 addSubview(deletedView!)
             }
-            deletedView = nil
         } else {
             if index >= 0, index < subviews.count {
                 subviews[index].removeFromSuperview()
             }
         }
+        deletedView = nil
     }
 
     /// 创建一个view