Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MidiUnavailableException in Java?

Tags:

java

midi

audio

I'm having some trouble playing MIDI files in Java. What I get is a MidiUnavailableException (MIDI OUT transmitter not available) when I try to play it. My code is standard:

try {
    midiseq = MidiSystem.getSequencer();
    midiseq.open();
    midiseq.setSequence(MidiSystem.getSequence(sound1));
    midiseq.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
    midiseq.start();
} catch (Exception e) {e.printStackTrace();}

midiseq is a Sequencer; sound1 is an InputStream.

This code works on several other computers, and even works in Eclipse and when used in a JAR file, just not when I launch it from the command prompt. I've downloaded a more stable Java midi application, and it threw the same exception.

It can't be a hardware problem because Eclipse can run it without problems, neither can it be a coding problem because this runs correctly on other computers. Even this one line of code throws this exception:

javax.sound.midi.MidiSystem.getSequencer();

Thanks in advance.

edit: My operating system is Windows Vista. Before I asked this question I've downloaded the latest JRE and JDK (1.6.0_13 I think).

edit: The code:

ClassLoader.getSystemClassLoader().loadClass("com.sun.media.sound.RealTimeSequencer");
System.out.println( "Sequencer class loaded" );// Otherwise ClassNotFoundException

prints "Sequencer class loaded."

But this code:

  try{
        System.out.println(javax.sound.midi.MidiSystem.getSequencer());
        System.exit(0);
      } catch(Exception e) {
        throw new RuntimeException(e);
      }
      System.exit(1);

throws the MidiUnavailableException.

Also, this code:

    MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
    if (devices.length == 0) {
        System.out.println("No MIDI devices found");
    } else {
        for (MidiDevice.Info dev : devices) {
            System.out.println(dev);
        }
    }

gives me this:

    Microsoft MIDI Mapper
    Microsoft GS Wavetable Synth
    Real Time Sequencer
    Java Sound Synthesizer
like image 997
Bai Li Avatar asked Apr 18 '09 03:04

Bai Li


5 Answers

First have a look at the capabilities of your system:

MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
if (devices.length == 0) {
    System.out.println("No MIDI devices found");
} else {
    for (MidiDevice.Info dev : devices) {
        System.out.println(dev);
    }
}

If there is nothing there, you might need to install soundbanks to enable the jvm to create a software sequencer.

Also check the output of MidiSystem.getReceiver() as the dreaded MidiUnavailableException will also occur if a receiver can not be created (MidiSystem javadoc).

I expect that installing the soundbank file will give the jvm the opportunity to fall back on a synthesizer receiver, and not require hardware access anymore (which seems to fail:)

like image 109
extraneon Avatar answered Nov 09 '22 10:11

extraneon


Is it possible that it has something to do with permissions? If you're running the JAR file as a different (more privileged) user than the plain command-line program, that might explain the difference. If it's not that, maybe some Java system property... I can't think of anything else that would make a difference between running the same code as a JAR file or as individual .class files.

It might help if you edit the question to provide more details about the different ways you've been running the program.

like image 37
David Z Avatar answered Nov 09 '22 11:11

David Z


WHen launching from the command prompt makes it fail, but launching from a JAR works, I'd check if you really use the same Java version for both cases. For the Jar, check file associations in Windows Explorer, and for the commandline, check whether java -version outputs the expected version (if not, dig through your PATH variable).

like image 27
mihi Avatar answered Nov 09 '22 11:11

mihi


Also (another shot in the dark), can you try and do a hard-shutdown on any other application that may be using sound and specifically Midi devices? I know that people get this problem on some Linuxes when another application uses Midi and doesn't release it.

like image 2
Uri Avatar answered Nov 09 '22 09:11

Uri


When you have JMF2.1.1e installed, remove all sound.jar files which are not only installed in the JMF directory but also in the jre and jdk directories.

http://www.coderanch.com/t/274513/java/java/javax-sound-midi-compatibility-JMF says:

The problem is the file sound.jar Find it in 3 places and delete it.

C:\Program Files\JMF2.1.1e\lib\sound.jar

C:\Program Files\Java\jdk1.6.0_07\jre\lib\ext\sound.jar

C:\Program Files\Java\jre1.6.0_07\lib\ext\sound.jar

Also, remove sound.jar reference from PATH and CLASSPATH This is an old sound.jar that messes up the new sound API in the new versions of Java.

like image 2
hfmanson Avatar answered Nov 09 '22 09:11

hfmanson