import UIKit
import Foundation
class PublicFunction: NSObject {
//MARK:document文檔目錄
static func documentPath() -> String {
let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as String
return documentPath
}
//MARK:下載文件路徑
static func downloadDirectory(path:String) -> String {
let downloadFile = PublicFunction.documentPath() + "/\(path)/"
let fileManager = FileManager.default
if fileManager.fileExists(atPath: downloadFile) == false {
do {
try fileManager.createDirectory(atPath: downloadFile, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
}
return downloadFile
}
}
import Foundation
import AVFoundation
class LocalRecorderTool: NSObject,AVAudioRecorderDelegate {
//單列
static let sharedAudioPlayer = LocalRecorderTool()
//錄音器
var recorder:AVAudioRecorder?
let semaphore = DispatchSemaphore(value: 1)
var audioPath:String = ""
var mp3Path:String = ""
//MARK:初始化錄音器
func createRecorderWith() {
let session = AVAudioSession.sharedInstance()
//在音頻播放前,首先創(chuàng)建一個異常捕捉語句
do {
//啟動音頻會話管理,此時會阻斷后臺音樂播放
try session.setActive(true)
//設(shè)置音頻播放類別,表示該應(yīng)用僅支持音頻播放
try session.setCategory(AVAudioSession.Category.playAndRecord)
//揚(yáng)聲器播放
try session.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
//錄音配置
let recordSettingDictionary:[String:Any] =
[
AVFormatIDKey: NSNumber(value: kAudioFormatLinearPCM),//音頻格式
AVNumberOfChannelsKey: 2, //錄音的聲道數(shù),立體聲為雙聲道
AVEncoderAudioQualityKey : AVAudioQuality.max.rawValue,//錄音質(zhì)量
AVLinearPCMBitDepthKey : 16,
AVSampleRateKey : 44100.0 //錄音器每秒采集的錄音樣本數(shù)
]
//錄音器
let playUrl = URL(string: audioPath)
recorder = try AVAudioRecorder.init(url: playUrl!, settings: recordSettingDictionary)
recorder?.delegate = self
recorder?.isMeteringEnabled = true
recorder?.prepareToRecord()
} catch {
printLog(message: error)
}
}
//MARK:開始錄音
func startRecording() {
let session = AVAudioSession.sharedInstance()
//在音頻播放前,首先創(chuàng)建一個異常捕捉語句
do {
//設(shè)置音頻播放類別,表示該應(yīng)用僅支持音頻播放
try session.setCategory(AVAudioSession.Category.playAndRecord)
recorder?.record()
} catch {
printLog(message: error)
}
}
//MARK:結(jié)束錄音
func stopRecording() {
recorder?.stop()
convertMp3.audioPCMtoMP3(self.audioPath, mp3File: self.mp3Path)
recorder = nil
}
//MARK:AVAudioRecord Delegate
func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
printLog(message: "finish recording---\(recorder)----\(flag)")
}
func audioRecorderEncodeErrorDidOccur(_ recorder: AVAudioRecorder, error: Error?) {
printLog(message: error)
}
}
//MARK:創(chuàng)建本地錄音器
func createLocalRecorder() {
//音頻地址
let savePath = PublicFunction.downloadDirectory(path: "record") + "play.caf"
LocalRecorderTool.sharedAudioPlayer.audioPath = savePath
LocalRecorderTool.sharedAudioPlayer.createRecorderWith()
}
//開始錄音
LocalRecorderTool.sharedAudioPlayer.startRecording()
//結(jié)束錄音
LocalRecorderTool.sharedAudioPlayer.stopRecording()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。