Browse Source

添加取消逻辑

jsonwang 3 years ago
parent
commit
b789a4e6f5
1 changed files with 38 additions and 20 deletions
  1. 38 20
      BFRecordScreenKit/Classes/BFVoiceRecordManager.swift

+ 38 - 20
BFRecordScreenKit/Classes/BFVoiceRecordManager.swift

@@ -25,6 +25,9 @@ class BFVoiceRecordManager: NSObject {
     //进度回调
  
     var recorderProgrossHandle: ((Float64?) -> Void)?
+    
+    //停止是否为取消操作
+    var isCancel:Bool = false
 
     /// 初始化方法
     /// - Parameters:
@@ -54,22 +57,23 @@ class BFVoiceRecordManager: NSObject {
 
     /// 取消音频录制
     func cancleRecord() {
-        stopRecord(isCancel: true)
-        cancelRecordHandle?(voiceModel)
+        stopRecord(cancel: true)
+        
     }
 
     /// 结束音频录制
     func endRecord() {
-        stopRecord(isCancel: false)
+        stopRecord(cancel: false)
     }
 
     /// 停止录制 1,正常停止 2,取消停止
     /// - Parameter isCancel: 是否为取消
-    func stopRecord(isCancel _: Bool) {
+     func stopRecord(cancel: Bool) {
 //        if !(audioRecorder?.voiceRecorder.isStarted() ?? false) {
 //            BFLog(message: "不是录制状态")
 //            return
 //        }
+        isCancel = cancel
         audioRecorder?.stopRecord()
     }
 }
@@ -85,29 +89,43 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate {
     public func recorderDidStop(_ outfile: String) {
         let duration = Date().timeIntervalSince(beginRecordTime)
         if duration > 1 {
-            // 处理降噪
-            let noiseFilePath = outfile.replacingOccurrences(of: ".wav", with: "_noise.wav")
-            BFLog(1, message: "降噪后地址:\(noiseFilePath) 原地址:\(outfile)")
-            NXNoiseReduction().denoise(outfile, outFile: noiseFilePath)
-            if let model = voiceModel {
-                model.wavFilePath = outfile
-                model.duration = "\(duration)"
-                endRecordHandle?(model, nil)
+            
+            if(isCancel){
+                // 删除录制的原文件
+                do {
+                    try FileManager.default.removeItem(atPath: outfile)
+                    print("取消后删除文件Success to remove recorder file. \(outfile)")
+                } catch {
+                    print("取消后删除文件Failed to remove recorder file. \(outfile)")
+                }
+                cancelRecordHandle?(nil)
+            }else{
+                
+                // 处理降噪
+                let noiseFilePath = outfile.replacingOccurrences(of: ".wav", with: "_noise.wav")
+                BFLog(1, message: "降噪后地址:\(noiseFilePath) 原地址:\(outfile)")
+                NXNoiseReduction().denoise(outfile, outFile: noiseFilePath)
+                if let model = voiceModel {
+                    model.wavFilePath = outfile
+                    model.duration = "\(duration)"
+                    endRecordHandle?(model, nil)
+                }
+
+    //            // 删除录制的原文件
+    //            do {
+    //                try FileManager.default.removeItem(atPath: outfile)
+    //                print("Success to remove recorder file. \(outfile)")
+    //            } catch {
+    //                print("Failed to remove recorder file. \(outfile)")
+    //            }
             }
 
-//            // 删除录制的原文件
-//            do {
-//                try FileManager.default.removeItem(atPath: outfile)
-//                print("Success to remove recorder file. \(outfile)")
-//            } catch {
-//                print("Failed to remove recorder file. \(outfile)")
-//            }
         } else {
             cShowHUB(superView: nil, msg: "说话时间太短")
             cancelRecordHandle?(voiceModel)
         }
     }
- 
+    
     public func eventCallback(_: BFRecorderManager, asrResult: String,audioFilePath:String) {
         subtitleRecordHandle?(asrResult, audioFilePath)
     }