Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4914667

Closing and reopening MIDI IN device on Linux throws MidiUnavailableException

XMLWordPrintable

    • b24
    • x86
    • linux

      Using the following program, after a variable numer of iterarions, a MidiUnavailableException is thrown on the call to [MidiDevice].open().

      If the try-catch block is uncommented (program sleeping for 2 seconds before the next open() call), no exception is thrown on 100 iterations.

      The same problem doesn't appear at all for MIDI OUT devices.

      exception:
      --------
      Exception in thread "main" javax.sound.midi.MidiUnavailableException: Device or resource busy
      at com.sun.media.sound.MidiInDevice.nOpen(Native Method)
      at com.sun.media.sound.MidiInDevice.implOpen(MidiInDevice.java:45)
      at com.sun.media.sound.AbstractMidiDevice.doOpen(AbstractMidiDevice.java:116)
      at com.sun.media.sound.AbstractMidiDevice.open(AbstractMidiDevice.java:88)
      at OpenClose2.main(OpenClose2.java:44)
      --------

      program:
      --------
      /*
       * OpenClose.java
       */

      import javax.sound.midi.*;


      /*
        run:
        java OpenClose2 100 in for 100 iterations on the MIDI IN device
        java OpenClose2 16 out for 16 iterations on the MIDI OUT device
      */
      public class OpenClose2 {

          public static void main(String[] args) throws Exception {
      int numIterations = Integer.parseInt(args[0]);
      MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
      MidiDevice outDevice = null;
      MidiDevice inDevice = null;
      out("available devices:");
      for (int i = 0; i < infos.length; i++) {
      out(infos[i].toString());
      MidiDevice device = MidiSystem.getMidiDevice(infos[i]);
      if (! (device instanceof Synthesizer)) {
      if (device.getMaxReceivers() != 0) {
      outDevice = device;
      }
      if (device.getMaxTransmitters() != 0) {
      inDevice = device;
      }
      }
      }
      out("Using MIDI OUT Device: " + outDevice);
      out("Using MIDI IN Device: " + inDevice);

      MidiDevice testDevice = null;
      if (args[1].equals("in")) {
      testDevice = inDevice;
      } else {
      testDevice = outDevice;
      }
      for (int i = 0; i < numIterations; i++) {
      out("@@@ ITERATION: " + i);
      testDevice.open();
      testDevice.close();

      // with the following code uncommented, the program runs slowly,
      // but without errors.

      // try {
      // Thread.sleep(2000);
      // } catch (InterruptedException e) {
      // }

      }
          }


          private static void out(String message) {
      System.out.println(message);
          }
      }


      /*** OpenClose2.java ***/
      --------

            duke J. Duke
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: