Java 中的 MidiUnavailableException?

发布于 2024-07-17 11:34:51 字数 1839 浏览 4 评论 0原文

我在 Java 中播放 MIDI 文件时遇到一些问题。 当我尝试播放它时,我得到的是 MidiUnavailableException(MIDI OUT 发射器不可用)。 我的代码是标准的:

try {
    midiseq = MidiSystem.getSequencer();
    midiseq.open();
    midiseq.setSequence(MidiSystem.getSequence(sound1));
    midiseq.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
    midiseq.start();
} catch (Exception e) {e.printStackTrace();}

midiseq是一个Sequencersound1 是一个InputStream

该代码可以在其他几台计算机上运行,​​甚至在 Eclipse 中以及在 JAR 文件中使用时也可以运行,只是当我从命令提示符启动它时就不行了。 我下载了一个更稳定的 Java midi 应用程序,它抛出了相同的异常。

它不可能是硬件问题,因为Eclipse可以毫无问题地运行它,也不可能是编码问题,因为它在其他计算机上运行正确。 即使这一行代码也会引发此异常:

javax.sound.midi.MidiSystem.getSequencer();

提前致谢。

编辑:我的操作系统是Windows Vista。 在我问这个问题之前,我已经下载了最新的 JRE 和 JDK(我认为是 1.6.0_13)。

编辑:代码:

ClassLoader.getSystemClassLoader().loadClass("com.sun.media.sound.RealTimeSequencer");
System.out.println( "Sequencer class loaded" );// Otherwise ClassNotFoundException

打印“已加载序列器类”。

但是这段代码:

  try{
        System.out.println(javax.sound.midi.MidiSystem.getSequencer());
        System.exit(0);
      } catch(Exception e) {
        throw new RuntimeException(e);
      }
      System.exit(1);

抛出 MidiUnavailableException。

另外,这段代码:

    MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
    if (devices.length == 0) {
        System.out.println("No MIDI devices found");
    } else {
        for (MidiDevice.Info dev : devices) {
            System.out.println(dev);
        }
    }

给了我这个:

    Microsoft MIDI Mapper
    Microsoft GS Wavetable Synth
    Real Time Sequencer
    Java Sound Synthesizer

I'm having some trouble playing MIDI files in Java. What I get is a MidiUnavailableException (MIDI OUT transmitter not available) when I try to play it. My code is standard:

try {
    midiseq = MidiSystem.getSequencer();
    midiseq.open();
    midiseq.setSequence(MidiSystem.getSequence(sound1));
    midiseq.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
    midiseq.start();
} catch (Exception e) {e.printStackTrace();}

midiseq is a Sequencer; sound1 is an InputStream.

This code works on several other computers, and even works in Eclipse and when used in a JAR file, just not when I launch it from the command prompt. I've downloaded a more stable Java midi application, and it threw the same exception.

It can't be a hardware problem because Eclipse can run it without problems, neither can it be a coding problem because this runs correctly on other computers. Even this one line of code throws this exception:

javax.sound.midi.MidiSystem.getSequencer();

Thanks in advance.

edit: My operating system is Windows Vista. Before I asked this question I've downloaded the latest JRE and JDK (1.6.0_13 I think).

edit: The code:

ClassLoader.getSystemClassLoader().loadClass("com.sun.media.sound.RealTimeSequencer");
System.out.println( "Sequencer class loaded" );// Otherwise ClassNotFoundException

prints "Sequencer class loaded."

But this code:

  try{
        System.out.println(javax.sound.midi.MidiSystem.getSequencer());
        System.exit(0);
      } catch(Exception e) {
        throw new RuntimeException(e);
      }
      System.exit(1);

throws the MidiUnavailableException.

Also, this code:

    MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
    if (devices.length == 0) {
        System.out.println("No MIDI devices found");
    } else {
        for (MidiDevice.Info dev : devices) {
            System.out.println(dev);
        }
    }

gives me this:

    Microsoft MIDI Mapper
    Microsoft GS Wavetable Synth
    Real Time Sequencer
    Java Sound Synthesizer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

阳光的暖冬 2024-07-24 11:34:51

首先看看您的系统的功能:

MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
if (devices.length == 0) {
    System.out.println("No MIDI devices found");
} else {
    for (MidiDevice.Info dev : devices) {
        System.out.println(dev);
    }
}

如果没有任何功能,您可能需要安装 音库以启用
jvm 创建软件定序器。

还要检查 MidiSystem.getReceiver() 的输出,因为如果无法创建接收器,也会发生可怕的 MidiUnavailableException (MidiSystem javadoc)。

我希望安装 soundbank 文件将使 jvm 有机会依靠合成器接收器,并且不再需要硬件访问(这似乎失败了:)

First have a look at the capabilities of your system:

MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
if (devices.length == 0) {
    System.out.println("No MIDI devices found");
} else {
    for (MidiDevice.Info dev : devices) {
        System.out.println(dev);
    }
}

If there is nothing there, you might need to install soundbanks to enable the
jvm to create a software sequencer.

Also check the output of MidiSystem.getReceiver() as the dreaded MidiUnavailableException will also occur if a receiver can not be created (MidiSystem javadoc).

I expect that installing the soundbank file will give the jvm the opportunity to fall back on a synthesizer receiver, and not require hardware access anymore (which seems to fail:)

淡莣 2024-07-24 11:34:51

有没有可能和权限有关系? 如果您以与普通命令行程序不同(权限更高)的用户身份运行 JAR 文件,则可能可以解释其中的差异。 如果不是这样,也许是一些 Java 系统属性...我想不出还有什么其他东西可以在将相同的代码作为 JAR 文件或单独的 .class 文件运行之间产生区别。

如果您编辑问题以提供有关运行程序的不同方式的更多详细信息,可能会有所帮助。

Is it possible that it has something to do with permissions? If you're running the JAR file as a different (more privileged) user than the plain command-line program, that might explain the difference. If it's not that, maybe some Java system property... I can't think of anything else that would make a difference between running the same code as a JAR file or as individual .class files.

It might help if you edit the question to provide more details about the different ways you've been running the program.

她如夕阳 2024-07-24 11:34:51

当从命令提示符启动导致失败,但从 JAR 启动有效时,我会检查您是否确实在这两种情况下使用相同的 Java 版本。 对于 Jar,检查 Windows 资源管理器中的文件关联,对于命令行,检查 java -version 是否输出预期的版本(如果没有,请挖掘 PATH 变量)。

WHen launching from the command prompt makes it fail, but launching from a JAR works, I'd check if you really use the same Java version for both cases. For the Jar, check file associations in Windows Explorer, and for the commandline, check whether java -version outputs the expected version (if not, dig through your PATH variable).

一张白纸 2024-07-24 11:34:51

另外(另一个黑暗中的镜头),您可以尝试对可能使用声音的任何其他应用程序(特别是 Midi 设备)进行硬关闭吗? 我知道当另一个应用程序使用 Midi 并且不发布它时,人们会在某些 Linux 上遇到此问题。

Also (another shot in the dark), can you try and do a hard-shutdown on any other application that may be using sound and specifically Midi devices? I know that people get this problem on some Linuxes when another application uses Midi and doesn't release it.

牵强ㄟ 2024-07-24 11:34:51

安装 JMF2.1.1e 后,删除所有 sound.jar 文件,这些文件不仅安装在 JMF 目录中,而且还安装在 jre 和 jdk 目录中。

http://www.coderanch.com/t /274513/java/java/javax-sound-midi-compatibility-JMF 说:

问题是文件 sound.jar 在 3 个地方找到它并删除它。

C:\Program Files\JMF2.1.1e\lib\sound.jar

C:\Program Files\Java\jdk1.6.0_07\jre\lib\ext\sound.jar

C:\Program Files\Java\jre1.6.0_07 \lib\ext\sound.jar

另外,从 PATH 和 CLASSPATH 中删除 sound.jar 引用
这是一个旧的 sound.jar,它扰乱了新版本 Java 中的新声音 API。

When you have JMF2.1.1e installed, remove all sound.jar files which are not only installed in the JMF directory but also in the jre and jdk directories.

http://www.coderanch.com/t/274513/java/java/javax-sound-midi-compatibility-JMF says:

The problem is the file sound.jar Find it in 3 places and delete it.

C:\Program Files\JMF2.1.1e\lib\sound.jar

C:\Program Files\Java\jdk1.6.0_07\jre\lib\ext\sound.jar

C:\Program Files\Java\jre1.6.0_07\lib\ext\sound.jar

Also, remove sound.jar reference from PATH and CLASSPATH
This is an old sound.jar that messes up the new sound API in the new versions of Java.

[浮城] 2024-07-24 11:34:51

你能添加你的操作系统和java版本吗?

并发布异常; Java 通常能够很好地指出错误所在。 只是并不总是容易理解:)

在我的 mac 上使用 java 1.5.0_16 我没有得到异常(在命令行上)。

以下程序

public class MidiTester {

    public static void main(String[] args) {
      try{
        System.out.println(javax.sound.midi.MidiSystem.getSequencer());
        System.exit(0);
      } catch(Exception e) {
        throw new RuntimeException(e);
      }
      System.exit(1);
    }
}

打印

com.sun.media.sound.RealTimeSequencer@d08633

You can check if you have theequencer class on the classpath:

ClassLoader.getSystemClassLoader().loadClass("com.sun.media.sound.RealTimeSequencer");
System.out.println( "Sequencer class loaded" );// Otherwise ClassNotFoundException

这至少可以清楚地表明您是否没有这些类(在通过 PATH 访问的 java 中),或者是否有不同的情况,例如权限问题。

Could you add your operating system and java version?

And post the exception; Java generally does a fair job saying whats wrong. It's just not always easy to understand :)

On my mac with java 1.5.0_16 I don't get an exception (on the command line).

The following program

public class MidiTester {

    public static void main(String[] args) {
      try{
        System.out.println(javax.sound.midi.MidiSystem.getSequencer());
        System.exit(0);
      } catch(Exception e) {
        throw new RuntimeException(e);
      }
      System.exit(1);
    }
}

prints

com.sun.media.sound.RealTimeSequencer@d08633

You could check whether you have the sequencer class on the classpath:

ClassLoader.getSystemClassLoader().loadClass("com.sun.media.sound.RealTimeSequencer");
System.out.println( "Sequencer class loaded" );// Otherwise ClassNotFoundException

This would at least make it clear whether you do not have the classes (in the java accessed via the PATH), or whether it's something different like a permission problem.

压抑⊿情绪 2024-07-24 11:34:51

将资源管理器指向您的 JRE 文件夹(用于运行程序的 $PATH 上 bin 文件夹上方的文件夹),例如“C:\Program Files\Java\jre1.6.0”

进入 \lib 文件夹。 您看到一个名为“audio”的文件夹吗? 里面有什么东西吗?
如果没有,请转至 http://java.sun.com/products /java-media/sound/soundbanks.html

按照描述下载任何 MIDI 库,并将其复制到上述文件夹中。
让我们知道它是否有效!

Point explorer to your JRE folder (the one above the bin folder on the $PATH you are using to run the program) e.g. "C:\Program Files\Java\jre1.6.0"

Go into the \lib folder. Do you see a folder called "audio"? Does it have anything in?
If not, go to http://java.sun.com/products/java-media/sound/soundbanks.html

download any of the MIDI banks as described, and copy it into the said folder.
let us know if it works!

过去的过去 2024-07-24 11:34:51

我也发现了同样的问题:无法播放MIDI。 但我注意到,当我将 JDK 从 1.6.0_10 升级到 1.6.0_13 时,问题就开始了。

我使用简单的程序测试了两个 JDK:

public class MidiTester {

public static void main(String[] args) {
  try{
    System.out.println(javax.sound.midi.MidiSystem.getSequencer());
    System.exit(0);
  } catch(Exception e) {
    throw new RuntimeException(e);
  }
  System.exit(1);
}

}

JDK 1.6.0_10 给出了结果:
com.sun.media.sound.RealTimeSequencer@1bd747

JDK 1.6.0_13 给出了结果:
线程“main”中的异常 java.lang.RuntimeException: javax.sound.midi.MidiUnavailableException: MIDI OUT 发射器不可用
在 ivan.seaquest.MidiTester.main(MidiTester.java:10)
引起:javax.sound.midi.MidiUnavailableException:MIDI OUT 发射器不可用
在 com.sun.media.sound.AbstractMidiDevice.createTransmitter(AbstractMidiDevice.java:444)
在 com.sun.media.sound.AbstractMidiDevice.getTransmitter(AbstractMidiDevice.java:299)
在 javax.sound.midi.MidiSystem.getSequencer(MidiSystem.java:451)
在 javax.sound.midi.MidiSystem.getSequencer(MidiSystem.java:348)
在 ivan.seaquest.MidiTester.main(MidiTester.java:7)

我将在 Sun 创建一个错误。 我希望它有帮助...

I've found the same problem: unable to play MIDI. But I've noticed that the problem had started when I upgrade the JDK from 1.6.0_10 to 1.6.0_13.

I tested both JDKs with the simple program:

public class MidiTester {

public static void main(String[] args) {
  try{
    System.out.println(javax.sound.midi.MidiSystem.getSequencer());
    System.exit(0);
  } catch(Exception e) {
    throw new RuntimeException(e);
  }
  System.exit(1);
}

}

JDK 1.6.0_10 gave the result:
com.sun.media.sound.RealTimeSequencer@1bd747

JDK 1.6.0_13 gave the result:
Exception in thread "main" java.lang.RuntimeException: javax.sound.midi.MidiUnavailableException: MIDI OUT transmitter not available
at ivan.seaquest.MidiTester.main(MidiTester.java:10)
Caused by: javax.sound.midi.MidiUnavailableException: MIDI OUT transmitter not available
at com.sun.media.sound.AbstractMidiDevice.createTransmitter(AbstractMidiDevice.java:444)
at com.sun.media.sound.AbstractMidiDevice.getTransmitter(AbstractMidiDevice.java:299)
at javax.sound.midi.MidiSystem.getSequencer(MidiSystem.java:451)
at javax.sound.midi.MidiSystem.getSequencer(MidiSystem.java:348)
at ivan.seaquest.MidiTester.main(MidiTester.java:7)

I'm going to create a bug at Sun. I hope it helps...

独行侠 2024-07-24 11:34:51

我曾经也有过一样的问题。 该错误是由类路径中包含的 JMF 引起的。

I have had the same problem. The error was caused by JMF included in the classpath.

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