MidiSystem.getSequencer() returns Audio Device Unavailable
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();
}开发者_StackOverflow
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?
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
I'm no gonna try to explain the error, but I did the following to solve my problem that was similar to yours.
1. Install the timidity player
$ sudo apt-get install timidity
After the installation execute the player
$ aplaymidi -l
1 Port Client name Port name
2 14:0 Midi Through Midi Through Port-0
in the case of the command above doesn't work, you may have to restart the machine
2. Finishing with the instalation of alsa-oss ALSA (Advanced Linux Sound Architecture)
$ sudo apt-get install alsa-oss
3. Execute the file
$ aoss java MidiClassTest
If you use Eclipse or another IDE you might want to use, as instance
$ aoss eclipse
or use an alias
alias eclipse='aoss eclipse-indigo &'
My guess is:
- Your system has no hardware synthesizer (or maybe it does but your sound card driver doesn't recognise it.)
- The Java Sound Synthesizer fails to open because you have no soundbank installed
- You could still obtain an unconnected sequencer with
MidiSystem.getSequencer(false)
though that would not be very useful.
Applications like timidity will not be affected because they perform their own software synthesis.
It may be possible to use a virtual MIDI port to play through timidity from Java.
精彩评论