1.AVAudioEngine負(fù)責(zé):
? 管理所有的音頻節(jié)點(diǎn)(audio nodes)
? 連接所有的音頻節(jié)點(diǎn)使其運(yùn)作形成鏈條(active chains)
? 動(dòng)態(tài)的獲取(attach)和配置所有音頻的節(jié)點(diǎn)右钾。
? 開(kāi)啟和停止API
2.有多種node,負(fù)責(zé)實(shí)現(xiàn)不同的功能,而AVAudioEngine在運(yùn)行中可以自由組合箕戳、拆卸node,node經(jīng)由它們各自的輸入被連接到下一級(jí)的輸出国撵,從而可以靈活實(shí)現(xiàn)想要的效果陵吸,但是AVAudioEngine在運(yùn)行中不能拆卸(detach)inputNode和OutputNode,且disconectNodes(兩個(gè)node)時(shí)介牙,必須同時(shí)disconect掉前一個(gè)node的output和后一個(gè)node的input壮虫。由于沒(méi)有發(fā)現(xiàn)這些細(xì)節(jié),導(dǎo)致采用比較復(fù)雜的方式解決插扒耳機(jī)的問(wèn)題以至于觸發(fā)了新的問(wèn)題环础。
3.AVAudioEngine實(shí)例化后默認(rèn)有三個(gè)node囚似,分別為inputNode剩拢、outputNode,這兩個(gè)node不可以被detach谆构,還有一個(gè)可選的mainMixerNode裸扶。
4.在使用AVAudioEngine時(shí),inputNode搬素、outputNode分別對(duì)應(yīng)硬件的麥克風(fēng)和揚(yáng)聲器呵晨,需要檢查其采樣率和聲道數(shù),若采樣率為0熬尺,則不可用摸屠。
5.使用實(shí)例,實(shí)現(xiàn)錄音+耳返+音樂(lè)粱哼,且耳返和錄音結(jié)果中有混響效果:
inputNode(麥克風(fēng))
mixerNode(混音)–> reverbNode(混響)–> outputNode(輸出)
playerNode(播放背景音樂(lè))
// attach EngineNodes
self.input = self.engine.inputNode;// 輸入
self.output = self.engine.outputNode;// 輸出
[self.engine attachNode:self.reverb];// 混響
[self.engine attachNode:self.mixer];
[self.engine attachNode:self.player];
// make Engine Connections
[self.engine connect:self.player to:self.mixer fromBus:0 toBus:0 format:[file processingFormat]];
[self.engine connect:self.input to:self.mixer fromBus:0 toBus:1 format:[self.input inputFormatForBus:0]];
[self.engine connect:self.mixer to:self.reverb format:self.audioFormat];
[self.reverb installTapOnBus:0 bufferSize:8192 format:self.audioFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
if (self->_isRecording) {// write buffer to file
[weakSelf.audioFile writeFromBuffer:buffer error:&writeBufferError];
weakSelf.currentRecTime += buffer.frameLength/when.sampleRate;
}
}];
[self.engine connect:self.reverb to:self.output format:self.audioFormat];
// start && play
[self.engine prepare];
[self.engine startAndReturnError:&startError];