问题
I work on a MIDI-based project in Java and struggle to refresh the list of Midi Devices.
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.
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.
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.
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.
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();
}
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?
回答1:
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
回答2:
One way is to set up a system as follows:
shell script:
start program with input 1 // that means it's the receiver
while(true) {
start program with input 2 // that means it's the sender
wait for an amount of time
}
end shell script
inside the program is the following:
if code==1:
while(true) {
do whatever you have to do
at specified point in time:
read file "myfile"
assemble the info
}
if code==2:
read the device info
write the info to "myfile"
exit
来源:https://stackoverflow.com/questions/41123620/update-list-of-midi-devices-in-java