123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- //
- // BFVoiceRecordManager.swift
- // BFRecordScreenKit
- //
- // Created by 胡志强 on 2021/11/24.
- //
- import BFCommonKit
- import BFMediaKit
- import Foundation
- class BFVoiceRecordManager: NSObject {
- // 录音相关
- var audioRecorder: BFRecorderManager?
-
-
- //录音结束回调
- var endRecordHandle: ((PQVoiceModel?, Error?) -> Void)?
- //录音取消回调
- var cancelRecordHandle: ((PQVoiceModel?) -> Void)?
- //录音进度回调
- var recorderProgrossHandle: ((Float64?) -> Void)?
- //字幕的回调 参数1: 字幕数据 ,参数2 :对应的录音文件
- var subtitleRecordHandle: ((String?,String?) -> Void)?
-
- //开始录制时间
- var beginRecordTime: Date = Date()
- //音频文件模型
- var voiceModel: PQVoiceModel?
-
- //停止是否为取消操作
- var mIsCancel:Bool = false
-
- /// 初始化方法
- /// - Parameters:
- /// - token: NLS token
- /// - appid: NLS appid
- public init(token: String, appid: String) {
- super.init()
- audioRecorder = BFRecorderManager(token, appid: appid)
- audioRecorder?.delegate = self
- }
-
-
- /// 开始录音
- func startRecord() {
- var recorderFilePath = exportAudiosDirectory
-
- if !directoryIsExists(dicPath: recorderFilePath) {
- BFLog(message: "文件夹不存在 \(recorderFilePath)")
- createDirectory(path: recorderFilePath)
- }
- recorderFilePath.append("recorder_\(Date().timeIntervalSince1970).wav")
- BFLog(1, message: "开始录音 \(recorderFilePath)")
-
- BFLog(1, message: "开始录制")
- audioRecorder?.startRecord(recorderFilePath)
- beginRecordTime = Date()
- }
-
- /// 停止录制
- /// - Parameter isCancel: 是否为取消 ,取消操作会把录制的文件删除和字幕删除
- func stopRecord(isCancel: Bool) {
-
- mIsCancel = isCancel
- audioRecorder?.stopRecord()
- }
- }
- // MARK: - 录音机回调
- extension BFVoiceRecordManager: BFRecorderManagerDelegate {
- public func recorderProgress(_: BFRecorderManager, recoderTime: Double) {
- BFLog(message: "录音机进度:\(recoderTime)")
- recorderProgrossHandle?(recoderTime)
- }
- public func recorderDidStop(_ outfile: String) {
- let duration = Date().timeIntervalSince(beginRecordTime)
- if duration > 1 {
-
- if(mIsCancel){
- // 删除录制的原文件
- 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)")
- // }
- }
-
- } else {
- cShowHUB(superView: nil, msg: "说话时间太短")
- cancelRecordHandle?(voiceModel)
- }
- }
-
- public func eventCallback(_: BFRecorderManager, asrResult: String,audioFilePath:String) {
- subtitleRecordHandle?(asrResult, audioFilePath)
- }
- }
|