2010-03-23 63 views
1

如果我有一个存储在JAR中的soundbank,我将如何使用资源加载将该音频库加载到我的应用程序中?Java:在JAR中嵌入Soundbank文件

我试图将尽可能多的MIDI程序整合到jar文件中,而我必须添加的最后一件事是我使用的soundbank文件,因为用户不会安装soundbanks 。我试图把它放到我的jar文件中,然后用Class类中的getResource()加载它,但是我在soundbank上得到了一个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); 
} 

回答

1

这是我做了什么:

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

这对我的作品。