我尝试使用Java soundAPI实现一个简单的MIDI播放器。我是Java的新手,但不是MIDI编程。我有问题正确理解soundAPI。我想要做的是选择一个设备,打开它,向它发送短的MIDI消息,然后关闭它。当使用SoundAPI时,必须执行上述操作,并添加到那个必须抓住发射器和接收器,打开发射器并将消息发送到其接收器(是吗??)
我设法掌握了MIDI系统并列出了所有可用设备的属性,这对于初学者来说非常好。我可以打开设备,但在打开发射器时会抛出MidiUnavailableException
异常。我已经总结了我必须在下面的函数send_simple_message
中遵循的所有步骤。我有两个问题:
非常感谢任何帮助。
public int send_simple_message (int device_no, int byte_1, int byte_2, int byte_3) throws InvalidMidiDataException
{
MidiDevice Current_Device; // Refers to MIDI device
Transmitter Sender; // its Transmitter
Receiver Getter; // the sender of the transmitter
ShortMessage message; // MIDI message to be sent to Receiver
try
{ // Get requested device
Current_Device = MidiSystem.getMidiDevice (Device_Info [device_no]);
System.out.println ("Selected: " + Current_Device.getDeviceInfo ().getName ());
Current_Device.open();
System.out.println ("Opened: " + Current_Device.getDeviceInfo ().getName ());
Sender = Current_Device.getTransmitter (); // <== throws exception
System.out.println ("Transmitter: " + Sender.toString ());
Getter = Sender.getReceiver ();
message = new ShortMessage (byte_1, byte_2, byte_3);
Getter.send (message, -1);
Sender.close ();
Current_Device.close ();
} catch (MidiUnavailableException e)
{
System.err.println ("MIDI device is unavailable");
return MIDI_OPEN_NOT_AVAILABLE;
} // try..catch
return MIDI_ACTION_OK;
} // send_simple_message //