我正在使用 AVAudioEngine 来捕捉用户的声音,同时对声音应用一些实时效果(如混响或某事)。这是我的代码
import UIKit
import AVFoundation
class ViewController: UIViewController{
var audioEngine:AVAudioEngine!
var audioReverb:AVAudioUnitReverb!
var audioInputNode:AVAudioInputNode!
override func viewDidLoad() {
super.viewDidLoad()
let session = AVAudioSession.sharedInstance()
session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
session.setActive(true, error: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func recordWithReverb(sender: AnyObject) {
audioEngine = AVAudioEngine()
audioEngine.stop()
audioEngine.reset()
audioInputNode = audioEngine.inputNode
audioReverb = AVAudioUnitReverb()
audioReverb.loadFactoryPreset(.LargeRoom)
audioReverb.wetDryMix = 50
audioEngine.attachNode(audioReverb)
let inputFormat = audioReverb.inputFormatForBus(0)
audioEngine.connect(audioInputNode, to: audioReverb, format: inputFormat)
audioEngine.connect(audioReverb, to: audioEngine.outputNode, format: inputFormat)
audioEngine.startAndReturnError(nil)
}
}
当我使用带有麦克风的耳机(如耳塞)进行测试时,一切正常。但是当我使用其他一些没有麦克风的耳机时(所以我只能使用 iphone 上的麦克风作为输入源),耳机右喇叭的声音总是比耳机的左喇叭声音小。如何解决?
还有一个我想问的问题,当我写代码的时候
var audioEngine:AVAudioEngine!
var audioReverb:AVAudioUnitReverb!
var audioInputNode:AVAudioInputNode!
在函数“recordWithReverb”中,代码无法正常工作,我无法得到任何输出,为什么会发生这种情况?