clip.addLineListener( new LineListener() { public void update(LineEvent evt) { if (evt.getType() == LineEvent.Type.STOP) { evt.getLine().close(); } } });
clip.addLineListener(new LineListener() { public void update(LineEvent event) { if (event.getType() == LineEvent.Type.STOP) { event.getLine().close(); } } });
music = AudioSystem.getClip(); AudioInputStream ais = AudioSystem.getAudioInputStream(Sound.class.getResource("/sounds" + filename)); music.open(ais); music.addLineListener(new LineListener(){ public void update(LineEvent e){ if(e.getType() == LineEvent.Type.STOP){ e.getLine().close(); } } }); music.start();
clip.addLineListener( new LineListener() { public void update(LineEvent event) { if (event.getType() == LineEvent.Type.STOP) { event.getLine().close(); System.exit(0); // <------------ terminate the JVM } } });
public void update (LineEvent event) { if (event.getType().equals(LineEvent.Type.STOP)) { try { event.getLine().close(); } catch (Throwable t) { t.printStackTrace(); } System.out.printf("Playback ended!"); } }
// this just opens a line to play the sample Clip clip = AudioSystem.getClip(); clip.open( audioFormat, sounddata, 0, sounddata.length); clip.start(); // at this point, there is a native thread created 'behind the scenes' // unless I added this, it never goes away: clip.addLineListener( new LineListener() { public void update(LineEvent evt) { if (evt.getType() == LineEvent.Type.STOP) { evt.getLine().close(); } } });
final CountDownLatch clipDone = new CountDownLatch(1); clip.addLineListener(new LineListener() { @Override public void update(LineEvent event) { if (event.getType() == LineEvent.Type.STOP) { event.getLine().close(); clipDone.countDown(); } } }); // play the sound clip and wait until it is done clip.start(); clipDone.await();
final HashSet<Line> openLines = new HashSet<Line>(); ... DataLine myLine = getDataLine(); myLine.addListener(new LineListener(){ public void update(LineEvent evt) { if(evt.getType().equals(LineEvent.Type.START)) openLines.add(evt.getLine()); else if(evt.getType().equals(LineEvent.Type.STOP)) openLines.remove(evt.getLine()); } }); ... public boolean isLineStarted(DataLine l) { return openLines.contains(l); }
... code ... // specify the sound to play File soundFile = new File("pathToYouFile"); //this does the conversion stuff for you if you have the correct SPIs installed AudioInputStream inputStream = getSupportedAudioInputStreamFromInputStream(new FileInputStream(soundFile)); // load the sound into memory (a Clip) DataLine.Info info = new DataLine.Info(Clip.class, inputStream.getFormat()); Clip clip = (Clip) AudioSystem.getLine(info); clip.open(sound); // due to bug in Java Sound, explicitly exit the VM when // the sound has stopped. clip.addLineListener(new LineListener() { public void update(LineEvent event) { if (event.getType() == LineEvent.Type.STOP) { event.getLine().close(); System.exit(0); } } }); // play the sound clip clip.start(); ... code ...
public void playClip() throws Exception { final CountDownLatch playingFinished = new CountDownLatch(1); final Clip clip = (Clip) AudioSystem.getLine(...); clip.open(...); clip.addLineListener(new LineListener() { public void update(LineEvent event) { if (event.getType() == LineEvent.Type.STOP) { event.getLine().close(); playingFinished.countDown(); } } }); clip.start(); try { playingFinished.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }
/** * 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(); } }
event.getLine().close(); System.exit(0);
event.getLine().close(); System.exit(0);
if (event.getType() == LineEvent.Type.STOP) { System.out.println("stop"); event.getLine().close();
public void update(LineEvent event) { if (event.getType().equals(LineEvent.Type.STOP)) { event.getLine().close(); latch.countDown();
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(); } } } }
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; } } }