QQ里面的變聲功能是不是很搞笑浦马?想要實(shí)現(xiàn)這樣的功能脚线?其實(shí)很簡單嚷狞,只需要使用音頻引擎(AVAudioEngine)來改變節(jié)點(diǎn)的速率(AVAudioUnitTimePitch)绳姨、調(diào)整回聲(AVAudioUnitDistortion)和混響(AVAudioUnitReverb)的值就可以達(dá)到你想要的很多效果愈诚。
[TOC]
音頻錄制
初始化錄音器
//初始化錄音器
func setUpRecord() -> Void {
//創(chuàng)建錄音文件保存路徑
let url = self.getSavePath()
//創(chuàng)建錄音格式設(shè)置
let settingDic = self.getAudioSetting()
//創(chuàng)建錄音機(jī)
do{
try audioRecorder = AVAudioRecorder(url: url, settings: settingDic)
audioRecorder.delegate = self
audioRecorder.isMeteringEnabled = true
audioRecorder.prepareToRecord()
print("成功初始化")
}
catch{
print("初始化失敗")
}
}
獲取錄音權(quán)限
//獲取錄音權(quán)限. 返回YES為無拒絕,NO為拒絕錄音.
func canRecord() -> Bool {
var canR = false
if (UIDevice.current.systemVersion as NSString).floatValue >= 7.0 {
let audioSession = AVAudioSession.sharedInstance()
if (audioSession.responds(to: #selector(AVAudioSession.requestRecordPermission(_:)))) {
AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool)-> Void in
canR = granted
if granted {
print("granted")
} else{
print("not granted")
}
})
}
}else{
canR = true
}
return canR
}
長按開始錄音
//長按響應(yīng)函數(shù)
func longGesAction(longGes:UILongPressGestureRecognizer) -> Void {
switch longGes.state {
case .began:
NSLog("錄音開始")
//判斷下是否授權(quán)使用麥克風(fēng)
if self.canRecord() {
audioRecorder.record()
}
.....
音頻播放
創(chuàng)建AudioFile
AVAudioFile的初始化函數(shù) init(forReading fileURL: URL) throws 后面跟著 throws 關(guān)鍵詞她按,所以在調(diào)用是需要使用 Do Try Catch 來處理錯(cuò)誤。
do {
try audioFile = AVAudioFile(forReading: url)
} catch {
showAlert(Alerts.AudioFileError, message: String(describing: error))
}
節(jié)點(diǎn)速率炕柔、節(jié)距酌泰、回聲、混響的調(diào)整
在改變節(jié)點(diǎn)數(shù)據(jù)的時(shí)候匕累,你需要先申請一個(gè)音頻引擎陵刹,然后將修改的東西使用 attach() 函數(shù)加入到引擎中。
速率欢嘿、節(jié)距
let changeRatePitchNode = AVAudioUnitTimePitch()
//速率
changeRatePitchNode.rate = rate!
//節(jié)距
changeRatePitchNode.pitch = pitch!
回聲
let echoNode = AVAudioUnitDistortion()
echoNode.loadFactoryPreset(.multiEcho1)
混響
let revrebNode = AVAudioUnitReverb()
revrebNode.loadFactoryPreset(.cathedral)
revrebNode.wetDryMix = 50
做完這些過后衰琐,你還需要將這些節(jié)點(diǎn)用音頻引擎的 connect(_ node1: AVAudioNode, to node2: AVAudioNode, format: AVAudioFormat?) 方法拼接起來。
拼接所有節(jié)點(diǎn)
在這里我寫了一個(gè)多參數(shù)的函數(shù)來拼接
func connectAudioNodes(_ nodes: AVAudioNode...) {
for x in 0..<nodes.count-1 {
audioEngine.connect(nodes[x], to: nodes[x+1], format: audioFile.processingFormat)
}
}
調(diào)用:
connectAudioNodes(audioPlayerNode,changeRatePitchNode,audioEngine.outputNode) //記得這個(gè) audioEngine.outputNode 是必須要的炼蹦,相當(dāng)于上下文的結(jié)尾
啟動(dòng)播放引擎
在這里我加了一個(gè)計(jì)時(shí)器來停止播放
//啟動(dòng)引擎 schedule to play and start the engine!
audioPlayerNode.stop()
audioPlayerNode.scheduleFile(audioFile, at: nil) {
var delayInSeconds :Double = 0.0 //延遲秒數(shù)
if let lastRenderTime = self.audioPlayerNode.lastRenderTime, let playerTime = self.audioPlayerNode.playerTime(forNodeTime: lastRenderTime) {
if rate != nil {
delayInSeconds = Double(self.audioFile.length - playerTime.sampleTime)/Double(self.audioFile.processingFormat.sampleRate)/Double(rate!)
}else {
delayInSeconds = Double(self.audioFile.length - playerTime.sampleTime)/Double(self.audioFile.processingFormat.sampleRate)
}
}
//組裝一個(gè)定時(shí)器在播放完成時(shí)調(diào)用stopAudio schedule a stop timer for when audio finishes playing
self.stopTimer = Timer(timeInterval: delayInSeconds, target: self, selector: #selector(VideoPlayHelper.stopAudio), userInfo: nil, repeats: false)
RunLoop.main.add(self.stopTimer, forMode: RunLoopMode.defaultRunLoopMode)
}
do {
try audioEngine.start()
} catch {
showAlert(Alerts.AudioEngineError, message: String(describing: error))
return
}
audioPlayerNode.play()
當(dāng)然在停止的時(shí)候記得將 播放引擎羡宙、定時(shí)器、播放器都停止掉喲框弛。
func stopAudio() {
if audioPlayerNode != nil {
audioPlayerNode.stop()
}
if stopTimer != nil {
stopTimer.invalidate()
}
if audioEngine != nil {
audioEngine.stop()
audioEngine.reset()
}
}
Demo地址
GitHub地址 你如果覺得喜歡幫忙給個(gè)Star辛辨!謝謝!