1

如果我有一个存储在 JAR 中的音库,我将如何使用资源加载将该音库加载到我的应用程序中......?

我正在尝试将尽可能多的 MIDI 程序合并到 jar 文件中,我必须添加的最后一件事是我正在使用的音库文件,因为用户不会安装音库。我正在尝试将它放入我的 jar 文件中,然后在 Class 类中使用 getResource() 加载它,但是我在我知道有效的音库上收到了 InvalidMidiDataException。

这是代码,它在我的合成器对象的构造函数中:


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

1 回答 1

1

这就是我所做的:

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

这个对我有用。

于 2010-03-23T15:09:20.593 回答