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