views:

217

answers:

1

I've keep having an exception thrown, on and on. When i try to make a new Sequencer object, i keep getting the javax.sound.midi.MidiUnavailableException: Audio Device Unavailable exception. So, here's the code:

import javax.sound.midi.*;

public class MiniMusicPlayer1
{
  public static void main(String[] args)
  {
    try
    {
      Sequencer sequencer = MidiSystem.getSequencer();
      sequencer.open();
      Sequence seq = new Sequence(Sequence.PPQ, 4);
      Track track = seq.createTrack();
      for (int i = 5; i < 61; i += 4)
      {
        track.add(makeEvent(144, 1, i, 100, i));
        track.add(makeEvent(128, 1, i, 100, (i+2)));
      }
      sequencer.setSequence(seq);
      sequencer.setTempoInBPM(220);
      sequencer.start();            
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  public static MidiEvent makeEvent(int comd, int chan, int one, int two, int tick)
  {
    MidiEvent event = null;
    try
    {
      ShortMessage a = new ShortMessage();
      a.setMessage(comd, chan, one, two);
      event = new MidiEvent(a, tick);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return event;
  }
}

And here's the complete error (at compile):

javax.sound.midi.MidiUnavailableException: Audio Device Unavailable
    at com.sun.media.sound.MixerSynth.implOpen(MixerSynth.java:165)
    at com.sun.media.sound.AbstractMidiDevice.doOpen(AbstractMidiDevice.java:144)
    at com.sun.media.sound.AbstractMidiDevice.openInternal(AbstractMidiDevice.java:134)
    at com.sun.media.sound.AbstractMidiDevice.getReceiverReferenceCounting(AbstractMidiDevice.java:339)
    at javax.sound.midi.MidiSystem.getReceiver(MidiSystem.java:243)
    at javax.sound.midi.MidiSystem.getSequencer(MidiSystem.java:442)
    at javax.sound.midi.MidiSystem.getSequencer(MidiSystem.java:348)
    at MiniMusicPlayer1.main(MiniMusicPlayer1.java:9)

First i was unable to play MIDI files on my pc, but then i got it to work, so now i can play MIDI files, that's okay. I tried even to close every process which uses my sound card, but the error is still there. Anyone can help me?

A: 

Check you process list to see if there's any copies of the program running. If it was once working, odds are good that the line:

sequencer.open();

is failing because the sequencer has already been opened for exclusive access by another program. Possibly, that "other program" is just a copy of the program you're working still running for whatever reason. If so, then the program to shut down may be one of the "java" programs. It sounds like you've already experimented with shutting down other midi-programs.

Also, you might be opening the "wrong" MIDI device. The

Sequencer sequencer = MidiSystem.getSequencer();

Will only open the "default" device. This default device might be mis-configured or unavailable, while the device your working programs are using might be different. It's a slim chance that such a thing is happening, but you might want to code up something like

MidiSystem.getMidiDeviceInfo()

and browse through the returned devices as demonstrated in this excerpt

// Obtain information about all the installed synthesizers.
Vector synthInfos;
MidiDevice device;
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
for (int i = 0; i < infos.length; i++) {
    try {
        device = MidiSystem.getMidiDevice(infos[i]);
    } catch (MidiUnavailableException e) {
          // Handle or throw exception...
    }
    if (device instanceof Synthesizer) {
        synthInfos.add(infos[i]);
    }
}
// Now, display strings from synthInfos list in GUI.

which is explained in more detail at http://java.sun.com/docs/books/tutorial/sound/accessing-MIDI.html

Edwin Buck
The output i get from this code is:Real Time SequencerJava Sound SynthesizerI tried to strace the program, but i don't see the prog acessing anywhere near the sound device in /dev/.. .
ksemeks