[英]Java: Embedding Soundbank file in JAR
如果我在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);
}
這是我所做的:
synth.loadAllInstruments(
MidiSystem.getSoundbank(
getClass().getResourceAsStream(filePath)));
這個對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.