我在 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 中的一些错误。
下面是我的 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();
}
请注意,在创建新的 Devices
对象时第一次调用 refreshDevices()
是有效的,并且打印会获取可用设备的列表。只是后来没有。然而,在插入或拔出设备后重新启动程序会返回正确的新设备数量。
谁能提供解决方案吗?
最佳答案
现在有一个库 CoreMidi4J,可以正确支持 macOS 上的热插拔(以及其他一些功能)。我不是作者,但它似乎可以很好地满足我的需求。
关于java - 更新 Java 中的 Midi 设备列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41123620/