0

Been trying for a long time to send a sequence to a midi device with jFugue 5:

     MusicReceiver device = getDeviceByName("name");

     Player player = new Player(); 
     Pattern pattern = new Pattern("A");    

     device.sendSequence(player.getSequence(pattern));

Can't go beyong "Unhandled exception type MidiUnavailableException" on "device.sendSequence".

     static MidiDevice.Info getDeviceInfoByName(String name) {
        for (MidiDevice.Info info : MidiSystem.getMidiDeviceInfo()) {
          if (info.getName().equals(name)) {
            return info;
          }
        }
        return null;
      }

      static MusicReceiver getDeviceByName(String name) {
          return new MusicReceiver((MidiDevice) getDeviceInfoByName(name));
      }
Yan
  • 582
  • 2
  • 6
  • 22
  • Can you give your entire stack trace? – k_g Feb 23 '15 at 21:15
  • java.lang.ClassCastException: com.sun.media.sound.MidiInDeviceProvider$MidiInDeviceInfo cannot be cast to javax.sound.midi.MidiDevice – Yan Feb 23 '15 at 21:37

1 Answers1

0

You are trying to cast an instance of MidiDevice.Info that you get from your getDeviceByInfo to a MidiDevice. Replace your getDeviceByName function with the following:

static MusicReceiver getDeviceByName(String name)
        throws MidiUnavailableException {
    MidiDevice.Info info = getDeviceInfoByName(name);
    return new MusicReceiver(MidiSystem.getMidiDevice(info));
}
k_g
  • 4,333
  • 2
  • 25
  • 40
  • Exception in thread "main" java.lang.Error: Unresolved compilation problems: Unhandled exception type MidiUnavailableException – Yan Feb 23 '15 at 21:58
  • From the javadoc: "MidiUnavailableException - if the requested device is not available due to resource restrictions" -- so I'm guessing that there is something wrong with your particular MIDI device – k_g Feb 23 '15 at 22:23
  • But both "MidiSystem.getMidiDevice(info))" and "device.sendSequence(player.getSequence(pattern))" are underlined, I can't even compile, so how could this be a problem of a particular device – Yan Feb 23 '15 at 22:35
  • Sorry I forgot that MidiUnavailableException isn't a RuntimeException. This latest edit will fix that issue but you will need to throw a try/catch around `MusicReceiver device = getDeviceByName("name");` or do some other kind of error handling – k_g Feb 23 '15 at 22:39
  • javax.sound.midi.MidiUnavailableException: MIDI IN receiver not available at com.sun.media.sound.AbstractMidiDevice.createReceiver(Unknown Source) at com.sun.media.sound.AbstractMidiDevice.getReceiver(Unknown Source) – Yan Feb 23 '15 at 23:35
  • yes and the Exception for the try/catch is what I've posted above – Yan Feb 23 '15 at 23:39
  • OK, this sounds like a problem with your computer/MIDI system/Name of your MIDI device. – k_g Feb 23 '15 at 23:41
  • I can easily connect to it through different software but not through java code :S – Yan Feb 23 '15 at 23:47
  • Well, that's beyond the scope of your original question. Perhaps try another device? – k_g Feb 23 '15 at 23:48