Error mark / reset not supported when running .wav file

1

I created a button that, when pressed, played a sound, but I'm having problems because it throws this exception:

  

"mark / reset not supported",

I have no idea what can be and how to solve, can anyone help me?

I created an example class for you to help me:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.swing.JOptionPane;

public class Teste {
    private static String somPath = "src/pistola_som.wav";
    private static FileInputStream fis;
    public static void main(String[] args) throws FileNotFoundException {
        fis = new FileInputStream(somPath);
        tocarSom(fis, false);
    }
    public static void tocarSom(final InputStream somPath, final boolean restart) {
        try {
            //Obtem os dados sonoros
            AudioInputStream ais = AudioSystem.getAudioInputStream(somPath);

            //Carrega o formato do audio e cria uma linha
            AudioFormat af = ais.getFormat();
            DataLine.Info dataLineInfo = new DataLine.Info(Teste.class, ais.getFormat(),
                    ((int)ais.getFrameLength() * af.getFrameSize()));

            //Carrega o som para o dispositivo
            Clip clip = (Clip)AudioSystem.getLine(dataLineInfo);
            clip.addLineListener(new LineListener() {

                //Evento do Listener
                public void update(LineEvent e) {
                    if(e.getType() == LineEvent.Type.STOP) {
                        e.getLine().close();
                    }
                }
            });
            clip.open(ais);

            //Tocar som
            if(restart) {
                clip.loop(clip.LOOP_CONTINUOUSLY);
            } else {
                clip.loop(0);
            }
        } catch(Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Erro na reprodução do audio:\n" + e.getMessage(), "Zumbi ",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}
    
asked by anonymous 06.10.2017 / 16:51

1 answer

1

According to this SOEn response and the method documentation itself AudioSystem.getAudioInputStream(InputStream) ", the stream you provide to this method must support resources of mark / restart flow.

To ensure this, you should test whether the type of InputStream you are passing gives this support, using the markSupported() ", or "decorate" as a type BufferedInputStream :

public static void main(String[] args) throws FileNotFoundException {

    fis = new FileInputStream(somPath);

    BufferedInputStream bufferStream =  new  BufferedInputStream(fis);
    tocarSom(bufferStream, false);
}

Testing a .wav file in the much simpler method below, worked perfectly.

public static void play(InputStream filename) {

    try {

        Clip clip = AudioSystem.getClip();
        BufferedInputStream bufferStream = new BufferedInputStream(filename);
        clip.open(AudioSystem.getAudioInputStream(bufferStream));
        Thread.sleep(1000);
        clip.start();

    } catch (InterruptedException | LineUnavailableException | IOException | UnsupportedAudioFileException e) {
        e.printStackTrace();
    }
}
    
06.10.2017 / 17:14