AVAudioEngine を使用して、ユーザーの声をキャプチャし、それにいくつかのエフェクトを適用しています。ヘッドフォンのマイクで録音すると、すべてがうまくいきます。しかし、電話の内蔵マイクで録音し、ヘッドフォンで音を再生する場合、左側のイヤホンだけに音があり、内蔵マイクには単一チャンネル入力しかないようです。では、どうすればこの問題を解決できますか? これが私のコードの一部です:
func connectNode(){
engine.connect(engine.inputNode!, to: reverbNode, format:reverbNode.outputFormatForBus(0))
engine.connect(reverbNode, to: delayNode, format: delayNode.outputFormatForBus(0))
engine.connect(delayNode, to: distortion, format: distortion.outputFormatForBus(0))
engine.connect(distortion, to: engine.mainMixerNode, format: engine.mainMixerNode.outputFormatForBus(0))
engine.connect(player, to: engine.mainMixerNode, format: engine.mainMixerNode.outputFormatForBus(0))
}
func recordToFile(){
setSessionRecord()
recordSetting[AVFormatIDKey] = NSNumber(unsignedInt: UInt32(kAudioFormatMPEG4AAC))
recordSetting[AVNumberOfChannelsKey] = NSNumber(int: 2)
var file: AVAudioFile!
do{
try file = AVAudioFile(forWriting: URLFor(fileName)!, settings: recordSetting)
}catch let error as NSError{
print("error:\(error)")
}
engine.mainMixerNode.installTapOnBus(0, bufferSize: 1024, format: file.processingFormat) { (buffer, time) -> Void in
try! file.writeFromBuffer(buffer)
}
}
func playbackRecord(){
setSessionPlayback()
var file:AVAudioFile!
file = try! AVAudioFile(forReading:URLFor(fileName)!)
let audioFormat = file.processingFormat
let audioFrameCount = UInt32(file.length)
let audioFileBuffer = AVAudioPCMBuffer(PCMFormat: audioFormat, frameCapacity: audioFrameCount)
try! file.readIntoBuffer(audioFileBuffer, frameCount: audioFrameCount)
player.scheduleBuffer(audioFileBuffer, atTime: nil, options: .Interrupts, completionHandler: {print(self.player.stop())})
if(!player.playing){
player.play()
}else{
print("stop")
player.stop()
}
}