sourceLine.close();
System.out.println(soundbytes.toString()); sourceDataLine.drain(); sourceDataLine.close(); } catch (Exception e) { System.out.println("Not working in speakers...");
line.close();
listener.waitUntilDone(); } finally { clip.close();
if ( clip!=null ) { clip.stop(); clip.close(); } else { clip = AudioSystem.getClip();
clip.close(); System.out.println("stopped!");
/** * This is implemented to listen for any line events and closes the * clip if required. * @param event the line event to follow */ @Override public void update(LineEvent event) { if (event.getType().equals(LineEvent.Type.STOP)) { Line line = event.getLine(); line.close(); } }
import java.io.File; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.sound.sampled.FloatControl; ... public static void main(String[] args) { try { Clip clip = AudioSystem.getClip(); File file = new File("[Some *.wav file]"); AudioInputStream stream = AudioSystem.getAudioInputStream(file); clip.open(stream); // Control the volume FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); gainControl.setValue(-20.0f); clip.start(); // The clip won't keep the JVM up if you reach the "end" // so the sleep is merely to let it play in the demo Thread.sleep(1000); clip.close(); } catch(Exception e) { e.printStackTrace(); } }
} finally { soundLine.drain(); soundLine.close();
Try this import java.io.File; import javax.sound.sampled.*; public class ClipLeak{ public static void main(String[] args) throws Exception{ while(true){ File soundFile = new File("./sound.wav"); AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile); DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat()); Clip clip = (Clip) AudioSystem.getLine(info); clip.open(sound); clip.addLineListener(new LineListener(){ public void update(LineEvent event){ if(event.getType() == LineEvent.Type.STOP){ event.getLine().close(); } } }); clip.start(); Thread.sleep(2000); if (clip.isOpen()) { clip.close(); sound.close(); } } } }
import java.io.File; import javax.sound.sampled.*; public void play(File file) { try { final Clip clip = (Clip)AudioSystem.getLine(new Line.Info(Clip.class)); clip.addLineListener(new LineListener() { @Override public void update(LineEvent event) { if (event.getType() == LineEvent.Type.STOP) clip.close(); } }); clip.open(AudioSystem.getAudioInputStream(file)); clip.start(); } catch (Exception exc) { exc.printStackTrace(System.out); } }
import java.io.File; import javax.sound.sampled.*; public void play(File file) { try { final Clip clip = (Clip)AudioSystem.getLine(new Line.Info(Clip.class)); clip.addLineListener(new LineListener() { @Override public void update(LineEvent event) { if (event.getType() == LineEvent.Type.STOP) clip.close(); } }); clip.open(AudioSystem.getAudioInputStream(file)); clip.start(); } catch (Exception exc) { exc.printStackTrace(System.out); } }
line.close();
((int) stream.getFrameLength() * format.getFrameSize())); Clip clip = (Clip) AudioSystem.getLine(info); clip.close(); return clip.getBufferSize() / (clip.getFormat().getFrameSize() * clip.getFormat()
private static boolean play(final InputStream arquivo, int count) { try (final AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(arquivo)) { // Carrega o formato do audio e cria uma linha final AudioFormat audioFormat = audioInputStream.getFormat(); final DataLine.Info dataLineInfo = new DataLine.Info(Clip.class, audioInputStream.getFormat(), (int) audioInputStream.getFrameLength() * audioFormat.getFrameSize()); // Carrega o som para o dispositivo try (final Clip clip = (Clip) AudioSystem.getLine(dataLineInfo)) { // Evento do LineListener clip.addLineListener(e -> { if (LineEvent.Type.STOP.equals(e.getType())) { e.getLine().close(); } }); clip.open(audioInputStream); clip.loop(count); } return true; } catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) { LogUtils.generate(e); return false; } } }
private static boolean play(final InputStream arquivo, int count) { try (final AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(arquivo)) { // Carrega o formato do audio e cria uma linha final AudioFormat audioFormat = audioInputStream.getFormat(); final DataLine.Info dataLineInfo = new DataLine.Info(Clip.class, audioInputStream.getFormat(), (int) audioInputStream.getFrameLength() * audioFormat.getFrameSize()); // Carrega o som para o dispositivo try (final Clip clip = (Clip) AudioSystem.getLine(dataLineInfo)) { // Evento do LineListener clip.addLineListener(e -> { if (LineEvent.Type.STOP.equals(e.getType())) { e.getLine().close(); } }); clip.open(audioInputStream); clip.loop(count); } return true; } catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) { LogUtils.generate(e); return false; } } }