本文介绍了Java中的Midi设备更新列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从事Java中基于MIDI的项目的工作,并努力刷新Midi设备的列表.

I work on a MIDI-based project in Java and struggle to refresh the list of Midi Devices.

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

As far as i know MidiSystem.getMidiDeviceInfo(); should give me an Info[] Array. Nothing happens for me however. The Objects inside the Array stay the same when new devices are plugged in or out and so is it's length.

搜索Stackoverflow使我想到了这个已有6年历史的问题 .评论之一表明,可能是在OSX/macOS上.我还没有在Windows或Linux上尝试过我的程序,但是无论如何它应该可以在OSX/macOS上运行.

Searching Stackoverflow brought me to this 6 year old question. One of the comments suggests, that being on OSX/macOS might be the issue. I haven't tried my program on Windows or Linux yet, but it should work on OSX/macOS anyways.

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

Another comment suggests setting the cache time to something short with com.sun.media.sound.JDK13Services.setCachingPeriod(1); manually could fix this. Not on OSX/macOS however.

在Google上进行的进一步搜索将我带到了 openjdk错误报告,其中声称这是OSX/macOS的Apple方面的错误.

A further search on Google brought me to a openjdk bug report which claims that this is some bug on Apples side in OSX/macOS.

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

Below is a shortened version of my Devices class for verification, though i'm sure it should be correct.

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()是可行的,并且打印后会获得可用设备的列表.只是事后没有.插入或插入设备后重新启动程序将返回正确的新设备数量.

Note, that calling refreshDevices() for the first time when creating a new Devices Object works and printing gets me a list of available devices. Just not afterwards. Restarting the program after plugging devices in or out returns the correct new number of devices however.

任何人都可以提供解决方案吗?

Can anyone provide a solution to this?

推荐答案

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

There is now a library, CoreMidi4J, that correctly supports hot-plugging (as well as a few other things) on macOS. I am not the author, but it appears to work well for my needs.

https://github.com/DerekCook/CoreMidi4J

这篇关于Java中的Midi设备更新列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 20:21