【发布时间】:2020-10-03 20:58:14
【问题描述】:
我正在尝试使用 SPM 将我的项目更新到 AudioKit v5。据我在当前文档中看到的,您通过将麦克风附加到音频引擎输入来实例化麦克风。
但是,我错过了以前的 AudioKit.inputDevices(然后是 AKManager.inputDevices)。我以前可以选择我喜欢的麦克风。
如何在 iOS 上使用 AudioKit v5 选择特定的麦克风?
【问题讨论】:
标签: audiokit
我正在尝试使用 SPM 将我的项目更新到 AudioKit v5。据我在当前文档中看到的,您通过将麦克风附加到音频引擎输入来实例化麦克风。
但是,我错过了以前的 AudioKit.inputDevices(然后是 AKManager.inputDevices)。我以前可以选择我喜欢的麦克风。
如何在 iOS 上使用 AudioKit v5 选择特定的麦克风?
【问题讨论】:
标签: audiokit
截至 2020 年 11 月 6 日,您需要确保您使用的是 v5-develop 分支,因为 v5-main 仍然不支持 48K 采样率的硬件。
这是允许您根据调试描述选择麦克风的代码:
// AudioKit engine and node definitions
let engine = AudioEngine()
var mic : AudioEngine.InputNode!
var boost : Fader!
var mixer : Mixer!
// Choose device for microphone
if let inputs = AudioEngine.inputDevices {
// print (inputs) // Uncomment to see the possible inputs
let micSelection : String = "Front" // On a 2020 iPad pro you can also choose "Back" or "Top"
var chosenMic : Int = 0
var micTypeCounter : Int = 0
for microphones in inputs {
let micType : String = "\(microphones)"
if micType.range(of: micSelection) != nil {
chosenMic = micTypeCounter
}
// If we find a wired mic, prefer it
if micType.range(of: "Wired") != nil {
chosenMic = micTypeCounter
break
}
// If we find a USB mic (newer devices), prefer it
if micType.range(of: "USB") != nil {
chosenMic = micTypeCounter
break
}
micTypeCounter += 1
}
do {
try AudioEngine.setInputDevice(inputs[chosenMic])
} catch {
print ("Could not set audio inputs: \(error)")
}
mic = engine.input
}
Settings.sampleRate = mic.avAudioNode.inputFormat(forBus: 0).sampleRate // This is essential for 48Kbps
// Start AudioKit
if !engine.avEngine.isRunning {
do {
boost = Fader(mic)
// Set boost values here, or leave it for silence
// Connect mic or boost to any other audio nodes you need
// Set AudioKit's output
mixer = Mixer(boost) // You can add any other nodes to the mixer
engine.output = mixer
// Additional settings
Settings.audioInputEnabled = true
// Start engine
try engine.avEngine.start()
try Settings.setSession(category: .playAndRecord)
} catch {
print ("Could not start AudioKit: \(error)")
}
}
建议在 viewDidLoad 中添加音频路由更改通知:
// Notification for monitoring audio route changes
NotificationCenter.default.addObserver(
self,
selector: #selector(audioRouteChanged(notification:)),
name: AVAudioSession.routeChangeNotification,
object: nil)
这会调用
@objc func audioRouteChanged(notification:Notification) {
// Replicate the code for choosing the microphone here (the first `if let` block)
}
【讨论】:
音频套件 4 也是如此。
API 已更改。
看来你应该写:
guard let inputs = AKManager.inputDevices else{
print("NO AK INPUT devices")
return false
}
【讨论】: