有没有办法获取 Java 中所有 MIDI 设备的列表?
Is there a way to get a list of all MIDI devices in Java?
我正在尝试获取 Java 中的所有 MIDI 设备,这比我想象的要难...
我尝试了 for each()
循环,但它不起作用,因为我不知道 ArrayList
我在什么地方使用此方法。
这是我的代码:
package com.example.cmmp.midi;
import javax.sound.midi.*;
import javax.sound.midi.spi.MidiDeviceProvider;
import java.util.*;
@SuppressWarnings("unchecked")
public class MidiDeviceReceiver {
private static MidiSystem MidiDevice;
public static Sequencer getSequencer() throws MidiUnavailableException {
return MidiSystem.getSequencer ();
}
public static List<MidiDevice> getMidiDevices(){
javax.sound.midi.MidiDeviceReceiver midiDeviceReceiver = new javax.sound.midi.MidiDeviceReceiver () {
public MidiSystem MidiDevice;
public void send(MidiMessage message, long timeStamp) {
}
//close
public void close() {
}
public MidiDevice getMidiDevice() {
MidiDeviceProvider midi = new MidiDeviceProvider () {
public MidiDevice.Info[] getDeviceInfo() {
return new MidiDevice.Info[0];
}
public MidiDevice getDevice(MidiDevice.Info info) {
try {
MidiDevice = (MidiSystem) MidiSystem.getMidiDevice ( info );
} catch (MidiUnavailableException e) {
e.printStackTrace ();
}
return (javax.sound.midi.MidiDevice) MidiDevice;
}
};
return (javax.sound.midi.MidiDevice) MidiDevice;
}
};
return (List<javax.sound.midi.MidiDevice>) MidiDevice;
}
}
如有任何帮助,我们将不胜感激。
您可以像这样枚举所有 MidiDevices:
package org.example2;
import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
public class App2 {
public static void main(String[] args) throws MidiUnavailableException {
MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
for (MidiDevice.Info info : devices) {
try {
MidiDevice device = MidiSystem.getMidiDevice(info);
System.out.println(info.getDescription() + " " + device.toString());
} catch (MidiUnavailableException e) {
System.out.println("Device " + info.getDescription() + " is not available");
e.printStackTrace();
}
}
}
}
当然,这会为您提供调用该函数时存在的设备的静态列表——如果有人插入 in/unplugs 设备,则该列表将过时。
我正在尝试获取 Java 中的所有 MIDI 设备,这比我想象的要难...
我尝试了 for each()
循环,但它不起作用,因为我不知道 ArrayList
我在什么地方使用此方法。
这是我的代码:
package com.example.cmmp.midi;
import javax.sound.midi.*;
import javax.sound.midi.spi.MidiDeviceProvider;
import java.util.*;
@SuppressWarnings("unchecked")
public class MidiDeviceReceiver {
private static MidiSystem MidiDevice;
public static Sequencer getSequencer() throws MidiUnavailableException {
return MidiSystem.getSequencer ();
}
public static List<MidiDevice> getMidiDevices(){
javax.sound.midi.MidiDeviceReceiver midiDeviceReceiver = new javax.sound.midi.MidiDeviceReceiver () {
public MidiSystem MidiDevice;
public void send(MidiMessage message, long timeStamp) {
}
//close
public void close() {
}
public MidiDevice getMidiDevice() {
MidiDeviceProvider midi = new MidiDeviceProvider () {
public MidiDevice.Info[] getDeviceInfo() {
return new MidiDevice.Info[0];
}
public MidiDevice getDevice(MidiDevice.Info info) {
try {
MidiDevice = (MidiSystem) MidiSystem.getMidiDevice ( info );
} catch (MidiUnavailableException e) {
e.printStackTrace ();
}
return (javax.sound.midi.MidiDevice) MidiDevice;
}
};
return (javax.sound.midi.MidiDevice) MidiDevice;
}
};
return (List<javax.sound.midi.MidiDevice>) MidiDevice;
}
}
如有任何帮助,我们将不胜感激。
您可以像这样枚举所有 MidiDevices:
package org.example2;
import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
public class App2 {
public static void main(String[] args) throws MidiUnavailableException {
MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
for (MidiDevice.Info info : devices) {
try {
MidiDevice device = MidiSystem.getMidiDevice(info);
System.out.println(info.getDescription() + " " + device.toString());
} catch (MidiUnavailableException e) {
System.out.println("Device " + info.getDescription() + " is not available");
e.printStackTrace();
}
}
}
}
当然,这会为您提供调用该函数时存在的设备的静态列表——如果有人插入 in/unplugs 设备,则该列表将过时。