Swift AudioKit无法设置输入设备

db2dz4w8  于 2023-04-19  发布在  Swift
关注(0)|答案(1)|浏览(120)

我尝试使用AudioKit v5.6.1从麦克风读取,但我很难设置AudioEngine的输入设备.我正在测试运行iOS 16.4和Xcode v14.3的iPhone 12 mini.这里是我测试的代码:import Foundation导入AudioKit

public class AudioService {
    
    public static let instance = AudioService()
    
    private let engine: AudioEngine
    
    private init () {
        
        engine = AudioEngine()
        
        guard !engine.avEngine.isRunning else {
            print("Audio engine is already running")
            return
        }
        
        guard case let inputs = AudioEngine.inputDevices, inputs.isNotEmpty else {
            print("Could not find any input devices")
            return
        }
        
        let input = inputs[0]
        
        try! AudioEngine.setInputDevice(input)
        
        
    }
}

我只是在View中初始化此服务:

import SwiftUI

struct TestView: View {
    
    let audioService = AudioService.instance
    
    var body: some View {
        ...
    }
}

然而,当我调用AudioEngine.setInputDevice时,我收到了这个错误Foundation._GenericObjCError.nilError,但已经确认input不是nil。它有name = "iPhone Microphone"deviceID = "Built-In Microphone Bottom"。任何帮助都将不胜感激,因为查看AudioKit食谱,我看不出我可以做什么不同的事情。

col17t5w

col17t5w1#

在发布这篇文章后,我发现我在Info.plist中有一个错别字,我应该把NSMicrophoneUsageDescription放在那里。代码在修复这个问题后运行。

相关问题