|
@@ -12,74 +12,62 @@ import Foundation
|
|
|
class BFVoiceRecordManager: NSObject {
|
|
|
// 录音相关
|
|
|
var audioRecorder: BFRecorderManager?
|
|
|
- // 录音结果回调
|
|
|
+
|
|
|
+
|
|
|
+ //录音结束回调
|
|
|
var endRecordHandle: ((PQVoiceModel?, Error?) -> Void)?
|
|
|
- var recordHandle: ((PQVoiceModel?, Error?) -> Void)?
|
|
|
+ //录音取消回调
|
|
|
var cancelRecordHandle: ((PQVoiceModel?) -> Void)?
|
|
|
-
|
|
|
- var recorderFilePath: String = ""
|
|
|
+ //录音进度回调
|
|
|
+ var recorderProgrossHandle: ((Float64?) -> Void)?
|
|
|
+ //字幕的回调 参数1: 字幕数据 ,参数2 :对应的录音文件
|
|
|
+ var subtitleRecordHandle: ((String?,String?) -> Void)?
|
|
|
+
|
|
|
+ //开始录制时间
|
|
|
var beginRecordTime: Date = Date()
|
|
|
+ //音频文件模型
|
|
|
var voiceModel: PQVoiceModel?
|
|
|
- ///字幕的回调 参数1: 字幕数据 ,参数2 :对应的录音文件
|
|
|
- var subtitleRecordHandle: ((String?,String?) -> Void)?
|
|
|
- //进度回调
|
|
|
-
|
|
|
- var recorderProgrossHandle: ((Float64?) -> Void)?
|
|
|
|
|
|
//停止是否为取消操作
|
|
|
- var isCancel:Bool = false
|
|
|
-
|
|
|
+ var mIsCancel:Bool = false
|
|
|
+
|
|
|
/// 初始化方法
|
|
|
/// - Parameters:
|
|
|
- /// - token: NLS
|
|
|
- /// - appid: NLS
|
|
|
+ /// - token: NLS token
|
|
|
+ /// - appid: NLS appid
|
|
|
public init(token: String, appid: String) {
|
|
|
super.init()
|
|
|
audioRecorder = BFRecorderManager(token, appid: appid)
|
|
|
audioRecorder?.delegate = self
|
|
|
}
|
|
|
-
|
|
|
- /// 录制音频。 index初衷是记录录音顺序,废弃了
|
|
|
- func startRecord(index: Int) {
|
|
|
- recorderFilePath = exportAudiosDirectory
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+ /// 开始录音
|
|
|
+ func startRecord() {
|
|
|
+ var recorderFilePath = exportAudiosDirectory
|
|
|
+
|
|
|
if !directoryIsExists(dicPath: recorderFilePath) {
|
|
|
BFLog(message: "文件夹不存在 \(recorderFilePath)")
|
|
|
createDirectory(path: recorderFilePath)
|
|
|
}
|
|
|
- recorderFilePath.append("recorder_\(index)_\(Date().timeIntervalSince1970).wav")
|
|
|
+ recorderFilePath.append("recorder_\(Date().timeIntervalSince1970).wav")
|
|
|
BFLog(1, message: "开始录音 \(recorderFilePath)")
|
|
|
-
|
|
|
+
|
|
|
BFLog(1, message: "开始录制")
|
|
|
audioRecorder?.startRecord(recorderFilePath)
|
|
|
beginRecordTime = Date()
|
|
|
}
|
|
|
-
|
|
|
- /// 取消音频录制
|
|
|
- func cancleRecord() {
|
|
|
- stopRecord(cancel: true)
|
|
|
+
|
|
|
+ /// 停止录制
|
|
|
+ /// - Parameter isCancel: 是否为取消 ,取消操作会把录制的文件删除和字幕删除
|
|
|
+ func stopRecord(isCancel: Bool) {
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- /// 结束音频录制
|
|
|
- func endRecord() {
|
|
|
- stopRecord(cancel: false)
|
|
|
- }
|
|
|
-
|
|
|
- /// 停止录制 1,正常停止 2,取消停止
|
|
|
- /// - Parameter isCancel: 是否为取消
|
|
|
- func stopRecord(cancel: Bool) {
|
|
|
-// if !(audioRecorder?.voiceRecorder.isStarted() ?? false) {
|
|
|
-// BFLog(message: "不是录制状态")
|
|
|
-// return
|
|
|
-// }
|
|
|
- isCancel = cancel
|
|
|
+ mIsCancel = isCancel
|
|
|
audioRecorder?.stopRecord()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// MARK: - 录音机回调
|
|
|
-
|
|
|
extension BFVoiceRecordManager: BFRecorderManagerDelegate {
|
|
|
public func recorderProgress(_: BFRecorderManager, recoderTime: Double) {
|
|
|
BFLog(message: "录音机进度:\(recoderTime)")
|
|
@@ -90,7 +78,7 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate {
|
|
|
let duration = Date().timeIntervalSince(beginRecordTime)
|
|
|
if duration > 1 {
|
|
|
|
|
|
- if(isCancel){
|
|
|
+ if(mIsCancel){
|
|
|
// 删除录制的原文件
|
|
|
do {
|
|
|
try FileManager.default.removeItem(atPath: outfile)
|
|
@@ -109,17 +97,18 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate {
|
|
|
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)
|