Java:在JAR中嵌入Soundbank文件 [英] Java: Embedding Soundbank file in JAR

查看:108
本文介绍了Java:在JAR中嵌入Soundbank文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I have a soundbank stored in a JAR, how would I load that soundbank into my application using resource loading...?

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

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


推荐答案

这就是我所做的:

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

它对我有用。

这篇关于Java:在JAR中嵌入Soundbank文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆