简体   繁体   中英

AudioInputStream not working once exported as jar/zip

so I am trying to play sound (mp3 files) via the mp3spi library. I should add that this project is meant to run inside another program, (like and add-on), so it does not itself have any main classes except for a debug class. When I run the project from this debug class, everything works fine, yet when running it from inside the main program (I should add that the add-on is exported as a jar and as zip file, the zip file will be loaded in the main program) I get a javax.sound.sampled.UnsupportedAudioFileException. I think the issue has to do with the add-on not being correctly connected to the libraries, yet all the mp3spi jars are included in the lib folder.

We are using maven to build all the add-ons and the main program, so I also tried adding the class path to the manifest file. (In the maven-jar-plugin , like suggested in many other stack overflow posts) Somehow it seems to work for others, but not for me. Others also suggested to make the jar runnable, so I added the debug class as the main class and tried again, which did not work either.

Anyway, I am quite lost as I am running out of ideas to try. Any help would be much appreciated!

Here is the code that causes the error again:

 String path = soundFileMap.get(soundId);
    try {
        inputStream = AudioSystem.getAudioInputStream(new File(path));
    ... (rest not shown)

And here is the exception:

javax.sound.sampled.UnsupportedAudioFileException: format not supported
    at javax.sound.sampled.AudioSystem.getAudioInputStreamImpl(AudioSystem.java:1023) ~[tritonus share-0.3.7-2.jar:?]
    at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1005) ~[tritonus-share-0.3.7-2.jar:?]
    at jundl77.izou.izousound.outputplugin.SoundEngine.playSoundFile(SoundEngine.java:298) [classes/:?]
    at jundl77.izou.izousound.outputplugin.SoundEngine.run(SoundEngine.java:382) [classes/:?]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_20]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_20]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_20]

And here is what my maven-jar-plugin looks like:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <manifestEntries>
                <Plugin-Id>${plugin.id}</Plugin-Id>
                <Plugin-Class>${plugin.class}</Plugin-Class>
                <Plugin-Version>${plugin.version}</Plugin-Version>
                <Plugin-Provider>${plugin.provider}</Plugin-Provider>
                <Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>
            </manifestEntries>
            <manifest>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                <addClasspath>true</addClasspath>
                <mainClass>jundl77.izou.izousound.Debug</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

Anyway, once again, any help would be much appreciated.

It is better to create the AudioInputStream directly from a URL or File, not an InputStream. Given that the resource is packed in a Jar, you should use URL, as one cannot read a file from within a jar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM