Java设置MIDI输出接收器

时间:2015-02-18 22:35:59

标签: java midi javax.sound.midi

嘿我正在尝试将MIDI数据从Java类发送到通过USB连接的MIDI设备。我在2年前就这样做了一次并且它有效,但我不知道怎么能找到这个项目了。

示例Java代码运行正常;

   myMsg = new ShortMessage();
   myMsg.setMessage(ShortMessage.NOTE_ON, 0, 60, 93);
   timeStamp = -1;
   Receiver rcvr = MidiSystem.getReceiver();
   rcvr.send(myMsg, timeStamp);

简单的东西。设备上显示5行代码和消息。问题是,这样,只有标准设备被设置并准备好接收MIDI。我不能要求我的用户每次想要使用我的应用程序时都将欲望设备设置为标准设备。 (Receiver充当我连接的物理设备输入的输出目的地/端口)

我现在正尝试通过执行以下操作来设置接收器:

   MidiDevice.Info[] infoA=MidiSystem.getMidiDeviceInfo();//array where all the device info goes

    for (int x=0;x<infoA.length;x++){
        System.out.println("in "+infoA[x]); //this displays all the devices
        }
        MidiSystem.getMidiDevice(infoA[d]); //d is set to an integer that represents the item number on the device list of the device I wanna send MIDI to
        System.out.println(infoA[d]);//last check if the correct device is selected
        MidiDevice MidiOutDevice = MidiSystem.getMidiDevice(infoA[d]); //setting this to "my" device in order to set the Receiver
        maschineReceiver= MidiOutDevice.getReceiver();
        Sequencer MidiOutSequencer = MidiSystem.getSequencer(); 
        MidiOutSequencer.getTransmitter().setReceiver(maschineReceiver); //probably unnecessary last 2 line but I gave this a try in case it helps 

如果我现在maschineReceiver.send(myMsg, timeStamp);,根本没有任何事情发生。我也试过不同的设备,但它没有变得更好。我确信这可能是一件非常困难的事情,因为它是我2年前实际实现的,当时我的编码技巧很糟糕,但我现在无法找到错误,无论多久我重新阅读了Java文档,它无论我做什么都不会工作。

提前致谢

1 个答案:

答案 0 :(得分:1)

要为您的计划实际预留设备,您需要使用MidiDevice方法open

if (!(device.isOpen())) {
    try {
      device.open();
  } catch (MidiUnavailableException e) {
          // Handle or throw exception...
  }
}