Browse Source

1.监听耳机拔出

wenweiwei 3 năm trước cách đây
mục cha
commit
6cb67de282

+ 9 - 17
BFRecordScreenKit/Classes/BFVoiceRecordManager.swift

@@ -25,9 +25,7 @@ class BFVoiceRecordManager: NSObject {
     var NeoNuiDebugHandle: ((String?) -> Void)?
     // 录音机 dubug信息
     var AudioQueueRecoderDebugHandle: ((String?) -> Void)?
-
-    // 开始录制时间
-    var beginRecordTime: Date = Date()
+ 
     // 音频文件模型
     var voiceModel: PQVoiceModel?
     // 停止是否为取消操作
@@ -60,10 +58,13 @@ class BFVoiceRecordManager: NSObject {
             BFLog(message: "文件夹不存在 \(recorderFilePath)")
             createDirectory(path: recorderFilePath)
         }
+        
         recorderFilePath.append("recorder_\(Date().timeIntervalSince1970).pcm")
-        beginRecordTime = Date()
-        BFLog(1, message: "开始录音 \(recorderFilePath)  开始时间\(beginRecordTime)")
-        voiceModel?.wavFilePath = recorderFilePath
+        
+        let noiseFilePath = recorderFilePath.replacingOccurrences(of: ".pcm", with: "_noise.wav")
+        voiceModel?.wavFilePath = noiseFilePath
+
+        BFLog(1, message: "开始录音 \(recorderFilePath)")
         audioRecorder?.startRecord(recorderFilePath)
     }
 
@@ -92,10 +93,7 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate {
     }
 
     public func recorderDidStop(_ outfile: String) {
-        let duration = Date().timeIntervalSince(beginRecordTime)
-        BFLog(1, message: "开始录音结束时间\(Date()) 时长\(duration)")
-        if duration > 1 {
-            if mIsCancel {
+             if mIsCancel {
                 // 删除录制的原文件
                 deleteFile(outfile: outfile)
                 cancelRecordHandle?(nil)
@@ -115,7 +113,6 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate {
                 NXNoiseReduction().denoise(wavFilePath, outFile: noiseFilePath)
                 if let model = voiceModel {
                     model.wavFilePath = noiseFilePath
-                    model.duration = "\(duration)"
                     endRecordHandle?(model, nil)
 
                     BFLog(message: "降噪用时\(Date().timeIntervalSince(beginRecordTime1))")
@@ -123,16 +120,11 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate {
                 // 删除临时 wav 文件
                 deleteFile(outfile: wavFilePath)
             }
-
-        } else {
-            cancelRecordHandle?(voiceModel)
-        }
-
+ 
         // 其它逻辑写在上面 保证最后关开关。
         isStoping = false
     }
 
-    
     /// 删除文件
     /// - Parameter outfile: <#outfile description#>
     public func deleteFile(outfile:String) {

+ 6 - 4
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenController.swift

@@ -490,15 +490,14 @@ public class BFRecordScreenController: BFBaseViewController {
 
             let header = dicResult?["header"] as? [String: Any]
             let payload = dicResult?["payload"] as? [String: Any]
-
-            BFLog(1, message: "识别结果:) \((payload?["result"])!),taskId:\((header?["task_id"] as? String) ?? "taskId"), 识别时间:\(((payload?["begin_time"]) as? Int) ?? 0) ~ \(((payload?["time"]) as? Int) ?? 0) startTime:\(self?.recorderManager?.voiceModel?.startCMTime.seconds ?? 0.0)")
+            BFLog(1, message: "识别结果:) \((payload)!),taskId:\((header?["task_id"] as? String) ?? "taskId"), 识别时间:\(((payload?["begin_time"]) as? Int) ?? 0) ~ \(((payload?["time"]) as? Int) ?? 0) startTime:\(self?.recorderManager?.voiceModel?.startCMTime.seconds ?? 0.0)")
             DispatchQueue.main.async {
                 // 1,保存字幕数据 begin_time是开始出现文字的时间,time 是结束文字出现的时间 单位都为毫秒,都是相对于录制音频数据整段时间。self.recorderManager.voiceModel?.startCMTime.seconds 为开始的录制的时间,开始和结束都要加上这个时差
 
                 let newSubtitle = PQEditSubTitleModel()
                 // 任务全局唯一ID,请记录该值,便于排查问题。 每次 startRecorder 和 stopRecoder 之间  task_Id都不会变化
                 newSubtitle.taskID = (header?["task_id"] as? String) ?? ""
-                BFLog(1, message: "url:\(URL(fileURLWithPath: audioFilePath ?? "b").deletingPathExtension().lastPathComponent), wavpath:\(URL(fileURLWithPath: self?.recorderManager?.voiceModel?.wavFilePath ?? "aa").deletingPathExtension().lastPathComponent)")
+                BFLog(1, message: "对应关系:字幕所属地址:\((audioFilePath ?? "b").replacingOccurrences(of: documensDirectory, with: "")), 开始录音输入:\((self?.recorderManager?.voiceModel?.wavFilePath ?? "aa").replacingOccurrences(of: documensDirectory, with: ""))")
                 // 这里加300ms 是因为返回结果为了切到字,时长提前一些时间,具体时间官方没说和原音频有关系。这里我们先延后300ms 单位:毫秒。
                 if let audioUrl = audioFilePath, URL(fileURLWithPath: audioUrl).deletingPathExtension().lastPathComponent.contains(URL(fileURLWithPath: self?.recorderManager?.voiceModel?.wavFilePath ?? "aa").deletingPathExtension().lastPathComponent) {
                     newSubtitle.timelineIn = (self?.recorderManager?.voiceModel?.startCMTime ?? .zero) + CMTime(seconds: Float64((((payload?["begin_time"]) as? Int) ?? 0) + 300) / 1000.0, preferredTimescale: 1000)
@@ -548,8 +547,9 @@ public class BFRecordScreenController: BFBaseViewController {
         recorderManager?.endRecordHandle = { [weak self, weak recorderManager] voideModel, _ in
             if let sself = self, let model = voideModel, FileManager.default.fileExists(atPath: model.wavFilePath ?? "") {
                 // 加入到语音数组里
+
                 model.endCMTime = sself.currentAssetProgress
-                BFLog(1, message: "录制结束当前录音文件:\(model.wavFilePath ?? "")-\(model.startCMTime.seconds)-\(model.endCMTime.seconds)-\(model.endCMTime.seconds - model.startCMTime.seconds)")
+                BFLog(1, message: "对应关系:录制结束文件地址:\((model.wavFilePath ?? "").replacingOccurrences(of: documensDirectory, with: "")) 开始录音前地址:\((self?.recorderManager?.voiceModel?.wavFilePath ?? "aa").replacingOccurrences(of: documensDirectory, with: ""))-  \(model.startCMTime.seconds)-\(model.endCMTime.seconds)-\(model.endCMTime.seconds - model.startCMTime.seconds)")
                 /// 注:录音机回调的录音时长大于一秒,而业务逻辑计算的会小于一秒
                 if (model.endCMTime.seconds - model.startCMTime.seconds) < 1 {
                     // 取消录制
@@ -634,6 +634,8 @@ public class BFRecordScreenController: BFBaseViewController {
                 sself.currentPlayRecordIndex = -3 // 刚录音完,不需要播放录音
                 BFLog(3, message: "重置播放index-\(#function) = \(sself.currentPlayRecordIndex)")
                 recorderManager?.voiceModel = nil
+            } else {
+                BFLog(2, message: "数据出错!!!!\(voideModel?.wavFilePath ?? "")")
             }
         }
         recorderManager?.cancelRecordHandle = { [weak self] voiceModel in