MidiSystem.getSequencer() 返回音频设备不可用

发布于 2024-08-30 11:47:30 字数 1945 浏览 4 评论 0原文

我不断地抛出异常。 当我尝试创建新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魂牵梦绕锁你心扉 2024-09-06 11:47:31

检查进程列表以查看是否有正在运行的程序的任何副本。如果它曾经工作过,则很有可能该行:

sequencer.open();

失败,因为定序器已被另一个程序打开以进行独占访问。可能,“其他程序”只是您正在工作的程序的副本,无论出于何种原因仍在运行。如果是这样,那么要关闭的程序可能是“java”程序之一。听起来您已经尝试过关闭其他 midi 程序。

另外,您可能打开了“错误的”MIDI 设备。只会

Sequencer sequencer = MidiSystem.getSequencer();

打开“默认”设备。此默认设备可能配置错误或不可用,而您的工作程序使用的设备可能不同。发生这种情况的可能性很小,但您可能想要编写类似的代码

MidiSystem.getMidiDeviceInfo()

并浏览返回的设备,如本摘录中所示,

// 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.

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:

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

明月松间行 2024-09-06 11:47:31

我不会尝试解释该错误,但我执行了以下操作来解决与您的问题类似的问题。

1.安装timidity播放器

$ sudo apt-get install timidity

安装完后执行播放器

$ aplaymidi -l

1    Port    Client name                      Port name
2    14:0    Midi Through                     Midi Through Port-0

如果上面的命令不起作用,您可能需要重新启动计算机

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

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 &'

扶醉桌前 2024-09-06 11:47:31

我的猜测是:

  1. 您的系统没有硬件合成器(或者可能有,但您的声卡驱动程序无法识别它。)
  2. Java 声音合成器无法打开,因为您没有安装音库
  3. 您仍然可以获得未连接< /em> 带有 MidiSystem.getSequencer(false) 的音序器,尽管这不是很有用。

像 timidity 这样的应用程序不会受到影响,因为它们执行自己的软件合成。

可以使用虚拟 MIDI 端口来通过 Java 进行播放。

My guess is:

  1. Your system has no hardware synthesizer (or maybe it does but your sound card driver doesn't recognise it.)
  2. The Java Sound Synthesizer fails to open because you have no soundbank installed
  3. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文