Verdes Andrei
Verdes Andrei

Reputation: 31

IOException markreset not supported with Java Sound on export

Ok so i made a game in java and i exported it. In Eclipse everything works perfectly but when i export the jar there are some problems. When you collide with another rectangle it should play a sound (In eclipse it works but not exported).

Here is my class for sounds:

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

and i used java -jar ProjectZero.jar to open up the console while playing and here is the error i get when it should play a sound:

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)

I tried exporting the resources into the jar but no success.

I tried putting the resources folder in the same folder with the jar but it doesn't work either.

Upvotes: 1

Views: 348

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

Java Sound requires a repositionable input stream. Either use getResource(String) for an URL (out of which JS will create such a stream), or wrap the original stream to make it so.

E.G. copied from the 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!");
            }
        });
    }
}

See also the info. page.

Upvotes: 2

Related Questions