我正在尝试使用 swift3播放麦克风音频输入而不录制.我可以使用以下代码录制音频:
let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, with:AVAudioSessionCategoryOptions.defaultToSpeaker)
try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
audioRecorder.delegate = self
audioRecorder.isMeteringEnabled = true
audioRecorder.prepareToRecord()
audioRecorder.record()
然后在拿起录制的文件后最终播放:
audioPlayerNode.play()
但是我想跳过录音步骤直接从麦克风输入到音频输出(在这种情况下是播放器节点).它的功能就像一个真正的麦克风.我可以直接执行此操作还是需要存在临时文件?我梳理了AVFoundation文档,但我似乎无法掌握直接路由.任何想法或建议表示赞赏.谢谢!
最佳答案 您可以使用AVAudioEngine执行此操作,但您会听到反馈:
import AVFoundation
class ViewController: UIViewController {
let engine = AVAudioEngine()
override func viewDidLoad() {
super.viewDidLoad()
let input = engine.inputNode!
let player = AVAudioPlayerNode()
engine.attach(player)
let bus = 0
let inputFormat = input.inputFormat(forBus: bus)
engine.connect(player, to: engine.mainMixerNode, format: inputFormat)
input.installTap(onBus: bus, bufferSize: 512, format: inputFormat) { (buffer, time) -> Void in
player.scheduleBuffer(buffer)
}
try! engine.start()
player.play()
}
}