gpt4 book ai didi

ios - 使用 AVAudioEngine 录制音频文件

转载 作者:IT王子 更新时间:2023-10-29 05:44:00 25 4
gpt4 key购买 nike

我想使用 AVAudioEngine 录制音频文件。所以它会从麦克风到一个输出文件。稍后我会添加一些效果。但目前,我只想让它发挥作用。

任何人都可以向我提供步骤或示例代码以便我可以开始吗?

最佳答案

这是我的简单解决方案。输出文件将在文档目录中。请注意,为简洁起见,我删除了错误处理。

var engine = AVAudioEngine()
var file: AVAudioFile?
var player = AVAudioPlayerNode() // if you need play record later

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
file = AVAudioFile(forWriting: URLFor("my_file.caf")!, settings: engine.inputNode.inputFormatForBus(0).settings, error: nil)
engine.attachNode(player)
engine.connect(player, to: engine.mainMixerNode, format: engine.mainMixerNode.outputFormatForBus(0)) //configure graph
engine.startAndReturnError(nil)
}

func record() {
engine.inputNode.installTap(onBus: 0, bufferSize: 1024, format: engine.mainMixerNode.outputFormat(forBus: 0)) { (buffer, time) -> Void in
try! self.file?.write(from: buffer)
return
}
}

@IBAction func stop(sender: AnyObject) {
engine.inputNode.removeTap(onBus: 0)
}


func URLFor(filename: String) -> NSURL? {
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
return documentsDirectory.appendingPathComponent(filename)
}

关于ios - 使用 AVAudioEngine 录制音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26388730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com