Java: Embedding Soundbank file in JAR
If I have a soundbank stored in a JAR, how would I load that soundbank into my application using resource loading...?
I'm trying to consolidate as much of a MIDI program into the jar file as I can, and the last thing I have to add is the soundbank file I'm using, as users won't have the soundbanks installed. I'm trying to put it into my jar file, and then load it with getResource() in the Class class, but I'm getting an InvalidMidiDataException on a soundbank that I know is valid.
Here's the code, it's in the constructor for my synthesizer object:
try {
synth = MidiSystem.getSynthesizer();
channels = synth.getChannels();
instrument = MidiSystem.getSoundbank(this.getClass().getResource("img/soundbank-mid.gm")).getInstruments();
currentInstrument = instrument[0];
synth.loadInstrument(currentInstrument);
synth.open();
}开发者_运维知识库 catch (InvalidMidiDataException ex) {
System.out.println("FAIL");
instrument = synth.getAvailableInstruments();
currentInstrument = instrument[0];
synth.loadInstrument(currentInstrument);
try {
synth.open();
} catch (MidiUnavailableException ex1) {
Logger.getLogger(MIDISynth.class.getName()).log(Level.SEVERE, null, ex1);
}
} catch (IOException ex) {
Logger.getLogger(MIDISynth.class.getName()).log(Level.SEVERE, null, ex);
} catch (MidiUnavailableException ex) {
Logger.getLogger(MIDISynth.class.getName()).log(Level.SEVERE, null, ex);
}
This is what I have done:
synth.loadAllInstruments(
MidiSystem.getSoundbank(
getClass().getResourceAsStream(filePath)));
It works for me.
精彩评论