123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- //
- // BFVoiceRecordManager.swift
- // BFRecordScreenKit
- //
- // Created by 胡志强 on 2021/11/24.
- //
- import Foundation
- import BFCommonKit
- import BFFramework
- import BFVideoEditKit
- class BFVoiceRecordManager {
-
- // 录音相关
- var audioRecorder: NXAudioRecorder?
- var limitedDuration:Double = 600 // 限制录制时长
- var endRecordHandle : ((PQVoiceModel?, Error?) -> Void)?
- var cancelRecordHandle : ((Error?) -> Void)?
- var recorderFilePath : String = ""
- var beginRecordTime:Date = Date()
- var voiceModel : PQVoiceModel?
-
- /// 录制音频
- func startRecord(index:Int){
-
- recorderFilePath = exportAudiosDirectory
- if !directoryIsExists(dicPath: recorderFilePath) {
- BFLog(message: "文件夹不存在 \(recorderFilePath)")
- createDirectory(path: recorderFilePath)
- }
- recorderFilePath.append("recorder_\(index)_\(Date().timeIntervalSince1970).wav")
- BFLog(1, message: "开始录音 \(recorderFilePath)")
- do {
- try audioRecorder = NXAudioRecorder(path: recorderFilePath)
- } catch {
- BFLog(message: "录音准备失败,当前无法录音 \(error))")
- self.cancelRecordHandle?(error)
- return
- }
- // audioRecorder?.recorderProgross = { [weak self] timer in
- // // 所有小段加在一起 > 10 min 自动取消
- // let sumTime = timer + (self?.getAudioFileslDuration() ?? 0)
- // if sumTime >= (self?.limitedDuration ?? 600) {
- // cShowHUB(superView: nil, msg: "最长可录音10分钟")
- // self?.stopRecord(isCancel: false)
- // return
- // }
- // }
- BFLog(1, message: "开始录制")
- audioRecorder?.startRecord()
- beginRecordTime = Date()
- // beginRecordTime = Date().timeIntervalSince1970
- }
-
- /// 取消音频录制
- func cancleRecord(){
- stopRecord(isCancel: true)
- cancelRecordHandle?(nil)
- }
-
- /// 结束音频录制
- func endRecord(){
- stopRecord(isCancel: false)
- }
-
- // 取所有声音文件的时长
- func getAudioFileslDuration() -> Float64 {
- let duration: Float64 = 0.0
- // if recorderPart.cacheRecorderCount == 0 { return duration }
- // for fileURL in recorderPart.cacheRecorderFiles {
- // duration = duration + AVURLAsset(url: fileURL, options: avAssertOptions).duration.seconds
- // BFLog(message: "duration === \(duration)")
- // }
- return duration
- }
-
- /// 停止录制 1,正常停止 2,取消停止
- /// - Parameter isCancel: 是否为取消
- func stopRecord(isCancel: Bool) {
- if !(audioRecorder?.recorder.isRecording ?? false) {
- BFLog(message: "不是录制状态")
- return
- }
- audioRecorder?.stopRecord { [weak self] isSuccess, url in
- guard let strongSelf = self else { return }
- if strongSelf.getAudioFileslDuration() < 599, (Date().timeIntervalSince( strongSelf.beginRecordTime)) < 1 {
- cShowHUB(superView: nil, msg: "说话时间太短")
- }
- let duration = Date().timeIntervalSince( strongSelf.beginRecordTime)
- if isSuccess && !isCancel && duration > 1 {
- BFLog(1, message: "结束录音 结果:\(isSuccess) \n url is \(url)")
- // 处理降噪
- let noiseFilePath = url.replacingOccurrences(of: ".wav", with: "_noise_\(1)_.wav")
- BFLog(1, message: "降噪后地址:\(noiseFilePath)")
- NXNoiseReduction().denoise(url, outFile: noiseFilePath)
- if let model = self?.voiceModel{
- model.wavFilePath = noiseFilePath
- self?.endRecordHandle?(model, nil)
- }
- //
- // strongSelf.recorderPart.cacheRecorderFiles.append(URL(fileURLWithPath: noiseFilePath))
- // 删除录制的原文件
- do {
- try FileManager.default.removeItem(atPath: url)
- print("Success to remove recorder file. \(url)")
- } catch {
- print("Failed to remove recorder file. \(url)")
- }
- }
- }
- }
- // 合并 成 MP3文件
- func mergeToMP3file() {
- // isMergeIng = true
- // PQPlayerViewModel.mergeAudios(urls: recorderPart.cacheRecorderFiles) { [weak self] completURL in
- //
- // BFLog(message: "completURL is \(String(describing: completURL))")
- //
- // if completURL?.absoluteString.count ?? 0 > 0 {
- // self?.recorderPart.compliteMP3AudioFile = completURL?.relativePath ?? ""
- // self?.isMergeIng = false
- // }
- // }
- }
- }
|