我想知道是否有关于如何动态切换AudioKit.setInputDevice(...)
和mic.setDevice(...)
的输入以及动态AudioKit.setOutputDevice(...)
的最佳做法?
在.stop()
和AudioKit本身上围绕此类操作调用.start()
和AKMicrophone
是什么时候?
使用允许用户设置所需输入和输出设备的应用程序,我通常会收到很多错误,通常会导致在此之后没有音频功能正常运行。
从启动设置相同的自定义输入/输出设备可靠地工作。
这是一个用于切换输入和输出的Playground。您应该会发现,如果您动态更改输出,则输入不再起作用并传递音频。
//: ## Tracking Microphone Input
//:
import AudioKitPlaygrounds
import AudioKit
let mic = AKMicrophone()
//: Set the microphone device if you need to
if let inputs = AudioKit.inputDevices {
let inputNum = 0
try AudioKit.setInputDevice(inputs[inputNum])
try mic.setDevice(inputs[inputNum])
}
let silence = AKBooster(gain: 0)
AudioKit.output = silence
try AudioKit.start()
//: User Interface
import AudioKitUI
class LiveView: AKLiveViewController {
override func viewDidLoad() {
addTitle("Input / Output switching")
addTitle("Inputs")
/// Input picker
if let inputs = AudioKit.inputDevices {
for input in inputs {
let newButton = AKButton(title: input.name, callback: { _ in
do {
print("Setting mic to \(input.name)")
try mic.setDevice(input)
}
catch {
print(error)
}
do{
print("Setting audioInputDevice to \(input.name)")
try AudioKit.setInputDevice(input)
}catch{
print(error)
}
})
addView(newButton)
}
}
let rollingPlot = AKNodeOutputPlot(mic, frame: CGRect(x: 0, y: 0, width: 440, height: 200))
rollingPlot.plotType = .buffer
rollingPlot.shouldFill = true
rollingPlot.shouldMirror = true
rollingPlot.color = AKColor.red
rollingPlot.gain = 2
addView(rollingPlot)
addTitle("Outputs")
// Output Picker
if let outputs = AudioKit.outputDevices {
for output in outputs {
let newButton = AKButton(title: output.name, callback: { _ in
do {
print("Trying setOutputDevice to \(output.name)")
try AudioKit.setOutputDevice(output)
}
catch {
print(error)
}
})
addView(newButton)
}
}
}
}
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = LiveView()
谢谢!