Java: Embedding Soundbank file in JAR

女生的网名这么多〃 提交于 2019-12-11 12:52:56

问题


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

回答1:


This is what I have done:

synth.loadAllInstruments(
    MidiSystem.getSoundbank(  
        getClass().getResourceAsStream(filePath)));

It works for me.



来源:https://stackoverflow.com/questions/2500921/java-embedding-soundbank-file-in-jar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!