我如何将Midi消息发送到特定的Midi端口

时间:2018-07-08 14:28:29

标签: java midi javasound

我想将Note_On消息发送到称为LoopBe(Link to Site)的虚拟Midi接口。如何获取Receiver对象(Java)? 我尝试了下面的代码,但在rcvr.send()上收到了NullPointerException。

public class test {

public static Receiver rcvr;

public static void main(String[] args) throws InvalidMidiDataException, MidiUnavailableException {
    String scene = "Test";
    getReceiver();
    ShortMessage myMsg = new ShortMessage();
    // Nachricht Channel Note Lautstärke
    myMsg.setMessage(ShortMessage.NOTE_ON, 0, 1, 127);
    rcvr.send(myMsg, -1);
    System.out.println("Szene " + scene + " ausgelöst");
}

public static void getReceiver() throws MidiUnavailableException {
    MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
    for(Info devices : infos )
    {
        System.out.println(devices.getName() + " : " + devices.getDescription());
        if(devices.getName() == "LoopBe Internal MIDI" && devices.getDescription() == "No details available") {
            MidiDevice device = MidiSystem.getMidiDevice(devices); 
            rcvr =  device.getReceiver();
            System.out.println("Receiver: " + rcvr.toString());
        }
    }
  }
}

我尝试了rcvr = MidiSystem.getReceiver()并成功了,但是它将消息发送到com.sun.media.sound.MidiOutDevice$MidiOutReceiver@404b9385。

1 个答案:

答案 0 :(得分:1)

在使用设备之前,您应该open使用设备。