导出时Java Sound不支持IOException markreset

时间:2013-07-14 05:48:19

标签: java audio jar embedded-resource javasound

好的,所以我在java中制作了一个游戏并将其导出。在Eclipse中一切都很完美,但是当我导出jar时会出现一些问题。当你与另一个矩形碰撞时,它应该发出声音(在eclipse中它可以工作但不会导出)。

这是我的声音课程:

package sound;
import java.io.InputStream;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class GameSounds 
{
    static String hitPath = "/resources/8bit_bomb_explosion.wav";
    public static synchronized void hit()
    {
        try
        {
            InputStream audioInStream = GameSounds.class.getResourceAsStream(hitPath);
            AudioInputStream inputStream = AudioSystem.getAudioInputStream(audioInStream);
            Clip clip = AudioSystem.getClip();
            clip.open(inputStream);
            clip.start(); 
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

我使用java -jar ProjectZero.jar在播放时打开控制台,这是我在播放声音时得到的错误:

java.io.IOException markreset not supported
        at java.util.zip.InflaterInputStream.reset(Unknown Source)
        at java.io.FilterInputStream.reset(Unknown Source)
        at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unkno
wn Source)
        at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
        at sound.GameSounds.hit(GameSounds.java14)
        at main.Main.doLogic(Main.java136)
        at main.Main.run(Main.java100)
        at java.lang.Thread.run(Unknown Source)

我尝试将资源导出到jar中但没有成功。

我尝试将资源文件夹放在与jar相同的文件夹中,但它也不起作用。

1 个答案:

答案 0 :(得分:2)

Java Sound需要可重新定位的输入流。使用getResource(String)作为URL(其中JS将创建这样的流),或者包装原始流以使其成为现实。

E.G。从Java Sound info. page复制。

import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;

public class LoopSound {

    public static void main(String[] args) throws Exception {
        URL url = new URL(
            "http://pscode.org/media/leftright.wav");
        Clip clip = AudioSystem.getClip();
        // getAudioInputStream() also accepts a File or InputStream
        AudioInputStream ais = AudioSystem.
            getAudioInputStream( url );
        clip.open(ais);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // A GUI element to prevent the Clip's daemon Thread
                // from terminating at the end of the main()
                JOptionPane.showMessageDialog(null, "Close to exit!");
            }
        });
    }
}

另请参阅 info. page