有没有办法判断 MIDI 设备是否通过 iOS 上的 USB 连接?



我正在使用 CoreMIDI 通过 iOS 设备上的相机连接套件接收来自 MIDI 键盘的消息。我的应用程序是关于音高识别的。我希望以下功能是自动的:

默认情况下使用麦克风(已实现),如果连接了 MIDI 键盘,请改用该麦克风。

它可以了解如何使用默认驱动程序判断它是否是USB键盘。只需询问名为"USB-MIDI"的设备:

private func getUSBDeviceReference() -> MIDIDeviceRef? {
for index in 0..<MIDIGetNumberOfDevices() {
let device = MIDIGetDevice(index)
var name : Unmanaged<CFString>?
MIDIObjectGetStringProperty(device, kMIDIPropertyName, &name)
if name!.takeRetainedValue() as String == "USB-MIDI" {
return device
}
}
return nil
}

但不幸的是,有些USB键盘使用自定义驱动程序。我如何判断我是否正在查看其中之一?标准蓝牙和网络设备似乎始终在线。即使设备上打开了 Wifi 和蓝牙(奇怪?

我最终使用了USBLocationID.到目前为止,它适用于我测试的任何设备,没有用户抱怨。但我不希望很多用户使用我的应用程序的 MIDI 功能。

/// Filters all `MIDIDeviceRef`'s for USB-Devices
private func getUSBDeviceReferences() -> [MIDIDeviceRef] {
var devices = [MIDIDeviceRef]()
for index in 0..<MIDIGetNumberOfDevices() {
let device = MIDIGetDevice(index)
var list: Unmanaged<CFPropertyList>?
MIDIObjectGetProperties(device, &list, true)
if let list = list {
let dict = list.takeRetainedValue() as! NSDictionary
if dict["USBLocationID"] != nil {
devices.append(device)
}
}
}
return devices
}

最新更新