问题
I have the following code that tries to load a soundbank. I've tested the code on Windows 7 with JRE 1.6.03 and 1.6.43. The old version of the JRE (1.6.03) works fine but the new version throws an exception. What's going on?
public Main() {
try {
Synthesizer synth = MidiSystem.getSynthesizer();
synth.open();
BufferedInputStream soundBankStream = new BufferedInputStream(
getClass().getClassLoader().getResourceAsStream(
"soundbank.gm"));
synth.loadAllInstruments(MidiSystem.getSoundbank(soundBankStream));
} catch (MidiUnavailableException e) {
e.printStackTrace();
} catch (InvalidMidiDataException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
.
F:\>java -version
java version "1.6.0_03"
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) 64-Bit Server VM (build 1.6.0_03-b05, mixed mode)
F:\>java -jar Test2.jar
F:\>"C:\Program Files\Java\jre6\bin\java.exe" -version
java version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)
F:\>"C:\Program Files\Java\jre6\bin\java.exe" -jar Test2.jar
javax.sound.midi.InvalidMidiDataException: cannot get soundbank from stream
at javax.sound.midi.MidiSystem.getSoundbank(Unknown Source)
at com.gulshansingh.test.Main.<init>(Main.java:24)
at com.gulshansingh.test.Main.main(Main.java:14)
You can download the soundbanks from here: http://www.oracle.com/technetwork/java/soundbanks-135798.html
回答1:
Copy-Paste from Java Sound API: Soundbanks page
Java Sound API
This page provides different soundbanks which you can download and use with Java Sound. Soundbanks are necessary for correct operation of the internal software synthesizer that ships with Java Sound. By default, the Windows version of the J2RE does not ship with a soundbank, so you need to manually install one to use Java Sound's MIDI engine. Java Sound has a fallback mechanism that uses a hardware MIDI port if no soundbank is available, but it prevents reliable and consistent MIDI playback, so installation of a soundbank is recommended for Java Sound.
As you can see, Java Sound API need installation, so if you have installed a newer JDK, you have to redo the installation.
A bug seem to be near your problem: 4887447, a workaround exists but not if the banksound is in a JAR
回答2:
I have been having the same problem, and from what I have found, the latest versions of java (1.7+), the GM soundbanks are no longer supported
http://www.oracle.com/technetwork/java/javase/compatibility-417013.html#incompatibilities
The software synthesizer implementation in the Java Sound package has been replaced with an open sourced version. Due to the replacement, the following features were dropped: GM soundbank support, RMF file playback support, OSS (Open Sound System) support on the Linux platform. The new synthesizer implementation supports soundbanks in DLS and SoundFont (SF2) formats.
As it says, it supports the DLS and SF2 Soundbank formats (which I tried with what you were doing and it worked). Have a look around on the internet for these files such as here: http://www.ronimusic.com/smp_ios_dls_files.htm
来源:https://stackoverflow.com/questions/15996711/newer-version-of-jre-doesnt-load-soundbank-but-older-version-does