MidiSystem.getSequencer()返回音频设备不可用 [英] MidiSystem.getSequencer() returns Audio Device Unavailable

查看:310
本文介绍了MidiSystem.getSequencer()返回音频设备不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在不断抛出异常. 当我尝试制作一个新的Sequencer对象时,我不断收到javax.sound.midi.MidiUnavailableException:音频设备不可用异常. 所以,这是代码:

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)

首先我无法在PC上播放MIDI文件,但是随后我使它可以工作,所以现在我可以播放MIDI文件了,没关系. 我什至试图关闭所有使用声卡的进程,但错误仍然存​​在. 有人可以帮助我吗?

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?

推荐答案

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

I'm no gonna try to explain the error, but I did the following to solve my problem that was similar to yours.

1.安装胆小的播放器

$ sudo apt-get install timidity

安装后执行播放器

$ 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.完成alsa-oss ALSA(高级Linux声音体系结构)的安装

$ sudo apt-get install alsa-oss

3.执行文件

$ aoss java MidiClassTest

如果您使用Eclipse或其他IDE,则可能要作为实例使用

If you use Eclipse or another IDE you might want to use, as instance

$ aoss eclipse

或使用别名

alias eclipse='aoss eclipse-indigo &'

这篇关于MidiSystem.getSequencer()返回音频设备不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆