我使用Java开发基于MIDI的项目,并努力刷新Midi设备列表。

据我所知MidiSystem.getMidiDeviceInfo();应该给我一个Info[]数组。但是我什么也没有发生。插入或插入新设备时,阵列内的对象保持不变,因此长度也是如此。

搜索Stackoverflow将我带到this 6 year old question。评论之一表明,可能是在OSX / macOS上。我还没有在Windows或Linux上尝试过我的程序,但是无论如何它应该可以在OSX / macOS上运行。

另一条评论建议使用com.sun.media.sound.JDK13Services.setCachingPeriod(1);手动将缓存时间设置为较短时间可以解决此问题。但是不在OSX / macOS上。

在Google上进行的进一步搜索使我进入openjdk bug report,它声称这是OSX / macOS的Apple方面的一些错误。

下面是我的Devices类的简化版本,用于验证,尽管我确定它应该是正确的。

private Info[] devices;

public Devices() {
    refreshDeviceList();
    printDeviceList();
}

// DeviceList Functions
public Info[] getDeviceList() {
    return devices;
}

public void printDeviceList() {
    for (int i = 0; i < devices.length; i++) {
        System.out.println("Description: \t" + devices[i].getDescription());
        System.out.println("Name: \t\t" + devices[i].getName());
        System.out.println("Vendor: \t" + devices[i].getVendor());
        System.out.println("Version: \t" + devices[i].getVersion());
        System.out.println();
    }
}

public void refreshDeviceList() {
    devices = MidiSystem.getMidiDeviceInfo();
}

// DeviceFunctions
public String getDeviceDescription(int i) {
    return devices[i].getDescription();
}

public String getDeviceName(int i) {
    return devices[i].getName();
}

public String getDeviceVendor(int i) {
    return devices[i].getVendor();
}

public String getDeviceVersion(int i) {
    return devices[i].getVersion();
}


请注意,在创建新的refreshDevices()对象时第一次调用Devices是可行的,并且在打印时会获得可用设备的列表。只是事后没有。在插入或插入设备后重新启动程序将返回正确的新设备数量。

谁能提供解决方案?

最佳答案

现在有一个库CoreMidi4J,可以正确支持macOS上的热插拔(以及其他一些功能)。我不是作者,但它似乎可以很好地满足我的需求。

https://github.com/DerekCook/CoreMidi4J

关于java - Java中的Midi设备更新列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41123620/

10-12 16:23
查看更多