MidiSystem.getSequencer () returns Audio Device Unavailable
I have an exception that was thrown and so on. When I try to create a new Sequencer object, I keep getting javax.sound.midi.MidiUnavailableException: Audio Device Unavailable. 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 is the complete error (on compilation):
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)
At first I was unable to play MIDI files on my computer, but then I got it to work, so now I can play MIDI files, which is good. I tried to even close every process that is using my soundcard, but the error still exists. Can anyone help me?
a source to share
Check the process list to see if there are any copies of the program. If this ever worked, chances are good that the line:
sequencer.open();
does not work because the sequencer is already open for exclusive access by another program. It is possible that the "other program" is just a copy of the program that you are still running for some reason. If so, the program to close can be one of the "java" programs. It looks like you've already experimented with disabling other midi programs.
In addition, you may open the wrong MIDI device.
Sequencer sequencer = MidiSystem.getSequencer();
Opens only "default device". This default device may be misconfigured or unavailable, while the device your work programs are using may be different. This is a subtle chance that this is happening, but you can program something like
MidiSystem.getMidiDeviceInfo()
and view the returned devices as shown 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
a source to share
I'm not going to try to explain the error, but I did the following to solve my problem, which was similar to yours.
1. Install the shyness player
$ sudo apt-get install timidity
After installation, execute the player
$ aplaymidi -l
1 Port Client name Port name
2 14:0 Midi Through Midi Through Port-0
in case the above command doesn't work, you may need to restart your computer
2. Finishing with the installation of alsa-oss ALSA (Advanced Linux Sound Architecture)
$ sudo apt-get install alsa-oss
3. Execute the file
$ aoss java MidiClassTest
If you are using Eclipse or some other IDE you can use for example
$ aoss eclipse
or use an alias
alias eclipse='aoss eclipse-indigo &'
a source to share
My suggestion:
- Your system does not have a hardware synthesizer (or it may be happening, but the sound card driver does not recognize it.)
- Java sound synthesizer won't open because you don't have sound banner installed
- You can still get an unlinked sequencer from
MidiSystem.getSequencer(false)
, though that wouldn't be very useful.
Apps like shyness won't be affected because they do their own software synthesis.
It is possible to use a virtual MIDI port to play shyness from Java.
a source to share