Java 设置 MIDI Out 接收器

标签 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文档,无论我做什么都行不通。

提前致谢

最佳答案

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

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

关于Java 设置 MIDI Out 接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28595332/

相关文章:

android - Android中的虚拟合成器(基于jar) - 原生jni?

java - 仅通过特定应用共享我的应用

java - 为什么我不能在从数据库中获取的对象中使用持久化?

c++ - midifile : getDurationInSeconds is 0 for all events

swift - Audiokit Midi 多个虚拟端口

java - 暂停 Java Sequencer 重置节奏

java - 使用Java发送sysex消息

使用字符串生成器和拆分更改 java 电话格式

java - 让 ArrayList.get() 返回

javascript - MIDI.js - 无法更改乐器