Java: Embedding Soundbank file in JAR

Posted by Pyroclastic on Stack Overflow See other posts from Stack Overflow or by Pyroclastic
Published on 2010-03-23T15:01:52Z Indexed on 2010/03/23 15:03 UTC
Read the original article Hit count: 238

Filed under:
|
|

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);
 }

© Stack Overflow or respective owner

Related posts about java

Related posts about midi