mark/reset not supported error while running file. wav

Asked

Viewed 121 times

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 it can be or how to fix it, someone can 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);
        }
    }
}
  • Please present a [mcve] so that it is possible to test the problem.

  • ready, now decrease the code

1 answer

1


According to this reply from Soen and the method documentation itself AudioSystem.getAudioInputStream(InputStream), the flow you provide to this method should support mark/reset flow.

To ensure this, you should test whether the type of InputStream who is passing gives this support, through the method markSupported(), or "decorate" like a guy BufferedInputStream:

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

    fis = new FileInputStream(somPath);

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

Testing a file .wav in the much simpler method below, it 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();
    }
}
  • It now gave this error: java.lang.Illegalargumentexception: No line matching class Teste supporting format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian, and buffers of 211680 to 211680 bytes is supported. is it because of the audio?

  • 1

    @Pauloaleixo with the method I added in the answer, works normally. This problem should be elsewhere.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.