Java设置MIDI输出接收器。

3

嗨,我正在尝试将来自Java类的MIDI数据发送到通过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);

简单的东西。只需要五行代码,消息就能出现在设备上。问题是,这种方式只设置了标准设备,并且准备好接收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);,什么都不会发生。我也尝试了不同的设备,但情况并没有好转。我确信这不是一件非常困难的事情,因为两年前我还是个编码技能很差的人,但我现在无论怎么重读Java文档,都找不到错误,无论我怎么做都不起作用。
提前感谢你。

你不需要打开设备吗,MidiOutDevice.open(); - sasankad
非常感谢!我太蠢了,不应该当程序员 >.< 请回答问题,我会选中的! - user2875404
1个回答

1

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接