浏览代码

字幕回调添加音频文件路径

jsonwang 3 年之前
父节点
当前提交
134f29d1a4

+ 6 - 6
BFRecordScreenKit/Classes/BFVoiceRecordManager.swift

@@ -20,9 +20,9 @@ class BFVoiceRecordManager:NSObject {
     var recorderFilePath: String = ""
     var beginRecordTime: Date = Date()
     var voiceModel: PQVoiceModel?
-    
-    //字幕的回调
-    var subtitleRecordHandle: ((String?) -> Void)?
+  
+    ///字幕的回调 参数1: 字幕数据 ,参数2 :对应的录音文件
+    var subtitleRecordHandle: ((String?,String?) -> Void)?
     //进度回调
     var recorderProgrossHandle: ((Float64?) -> Void)?
     
@@ -91,7 +91,7 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate{
         if  duration > 1 {
        
             // 处理降噪
-            let noiseFilePath = outfile.replacingOccurrences(of: ".wav", with: "_noise_\(1)_.wav")
+            let noiseFilePath = outfile.replacingOccurrences(of: ".wav", with: "_noise.wav")
             BFLog(1, message: "降噪后地址:\(noiseFilePath) 原地址:\(outfile)")
             NXNoiseReduction().denoise(outfile, outFile: noiseFilePath)
             if let model = voiceModel {
@@ -110,9 +110,9 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate{
         }
     }
     
-    public func eventCallback(_: BFRecorderManager, asrResult: String) {
+    public func eventCallback(_: BFRecorderManager, asrResult: String,audioFilePath:String) {
         
-        subtitleRecordHandle?(asrResult)
+        subtitleRecordHandle?(asrResult, audioFilePath)
     }
 
 }

+ 7 - 3
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenController.swift

@@ -413,21 +413,24 @@ public class BFRecordScreenController: BFBaseViewController {
             }
             
             //录音字幕回调
-            self?.recorderManager?.subtitleRecordHandle = {[weak self] asrResult in
+            self?.recorderManager?.subtitleRecordHandle = {[weak self] asrResult ,audioFilePath in
                 if(asrResult == nil){
                     BFLog(message: "识别结果为空????不能生成字幕数据")
                     return
                 }
                 let dicResult: [String: Any]? = jsonStringToDictionary(asrResult!)
 
+                let header = dicResult?["header"] as? [String: Any]
                 let payload = dicResult?["payload"] as? [String: Any]
 
-                
+             
                 BFLog(message: "识别结果:) \((payload?["result"])!) startTime:\(self?.recorderManager?.voiceModel?.startTime ?? 0.0)")
                 DispatchQueue.main.async {
                     // 1,保存字幕数据 begin_time是开始出现文字的时间,time 是结束文字出现的时间 单位都为毫秒,都是相对于录制音频数据整段时间。self.recorderManager.voiceModel?.startTime 为开始的录制的时间,开始和结束都要加上这个时差
 
                     let newSubtitle = PQEditSubTitleModel()
+                    //任务全局唯一ID,请记录该值,便于排查问题。 每次 startRecorder 和 stopRecoder 之间  task_Id都不会变化
+                    newSubtitle.taskID = (header?["task_id"] as? String) ?? ""
                     
                     //这里加300ms 是因为返回结果为了切到字,时长提前一些时间,具体时间官方没说和原音频有关系。这里我们先延后300ms 单位:毫秒。
                     newSubtitle.timelineIn = (self?.recorderManager?.voiceModel?.startTime ?? 0.0) + Float64((((payload?["begin_time"]) as? Int) ?? 0) + 300) / 1000.0
@@ -439,8 +442,9 @@ public class BFRecordScreenController: BFBaseViewController {
                         showText += "..."
                     }
                     newSubtitle.text = showText
+                    newSubtitle.audioFilePath = audioFilePath ?? ""
 
-                    BFLog(message: "添加字幕数据 timelineIn \(newSubtitle.timelineIn) timelineOut \(newSubtitle.timelineOut) text \(newSubtitle.text)")
+                    BFLog(message: "添加字幕数据 timelineIn \(newSubtitle.timelineIn) timelineOut \(newSubtitle.timelineOut) text r\(newSubtitle.text) 音频路径为:\(audioFilePath ?? "")")
                     newSubtitle.setting = self?.subtitleSettingView.subtitle.setting ?? BFSubTitileSettingModel()
 
                     self?.itemModels[self?.currItemModelIndex ?? 0].titleStickers.append(newSubtitle)