【问题标题】:Is there a way to tell if a MIDI-Device is connected via USB on iOS?有没有办法判断 MIDI 设备是否通过 iOS 上的 USB 连接?
【发布时间】:2017-08-21 14:42:44
【问题描述】:

我正在使用 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 和蓝牙已关闭(奇怪?)。

【问题讨论】:

  • 您想尝试使用 kMIDIPropertyConnectionUniqueID 而不是 kMIDIPropertyName 我认为您可以使用它更好地定位。
  • 在下面查看我的回答,感谢您分享您的想法!

标签: ios swift midi coremidi


【解决方案1】:

我最终使用了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
    }

【讨论】:

    猜你喜欢
    • 2023-01-23
    • 2016-09-24
    • 1970-01-01
    • 2011-10-31
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多