MidiSystem.getSequencer() 返回音频设备不可用
我不断地抛出异常。 当我尝试创建新的 Sequencer 对象时,我不断收到 javax.sound.midi.MidiUnavailableException: Audio Device Unavailable 异常。 所以,这是代码:
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;
}
}
这是完整的错误(编译时):
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)
首先我无法在我的电脑上播放 MIDI 文件,但后来我让它工作,所以现在我可以播放 MIDI 文件,没关系。 我什至尝试关闭使用声卡的每个进程,但错误仍然存在。 有人可以帮助我吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查进程列表以查看是否有正在运行的程序的任何副本。如果它曾经工作过,则很有可能该行:
失败,因为定序器已被另一个程序打开以进行独占访问。可能,“其他程序”只是您正在工作的程序的副本,无论出于何种原因仍在运行。如果是这样,那么要关闭的程序可能是“java”程序之一。听起来您已经尝试过关闭其他 midi 程序。
另外,您可能打开了“错误的”MIDI 设备。只会
打开“默认”设备。此默认设备可能配置错误或不可用,而您的工作程序使用的设备可能不同。发生这种情况的可能性很小,但您可能想要编写类似的代码
并浏览返回的设备,如本摘录中所示,
在 http://java.sun.com/docs/books/tutorial/sound/accessing-MIDI.html
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:
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
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
and browse through the returned devices as demonstrated in this excerpt
which is explained in more detail at http://java.sun.com/docs/books/tutorial/sound/accessing-MIDI.html
我不会尝试解释该错误,但我执行了以下操作来解决与您的问题类似的问题。
1.安装timidity播放器
$ sudo apt-get install timidity
安装完后执行播放器
$ aplaymidi -l
如果上面的命令不起作用,您可能需要重新启动计算机
2。完成 alsa-oss ALSA(高级 Linux 声音架构)的安装
$ sudo apt-get install alsa-oss
3.执行文件
$ aoss java MidiClassTest
如果您使用 Eclipse 或您可能想要使用的其他 IDE,作为实例
$ aoss eclipse
或使用别名
别名 eclipse='aoss eclipse-indigo &'
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
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 &'
我的猜测是:
MidiSystem.getSequencer(false)
的音序器,尽管这不是很有用。像 timidity 这样的应用程序不会受到影响,因为它们执行自己的软件合成。
可以使用虚拟 MIDI 端口来通过 Java 进行播放。
My guess is:
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.