/** * Plays a sound from a file. * * @param filename Path to the sound file */ public static void playSound(String filename) { URL resource = ClassLoader.getSystemClassLoader().getResource(filename); try { final Clip clip = (Clip) AudioSystem.getLine(new Line.Info(Clip.class)); clip.addLineListener(event -> { if (event.getType() == LineEvent.Type.STOP) { clip.close(); } }); clip.open(AudioSystem.getAudioInputStream(resource)); clip.start(); } catch (Exception e) { LOGGER.error("Failed to play sound " + filename, e); } }
private SoundChain play(final Sound sound, boolean repeatedly, final SoundChain chain) { stop(sound); if(sound.disabled()) { chain.takeOver(); return chain; } try { InputStream stream = sound.inputStream(); //wrap the stream in another one that supports mark/reset (see issue #31) stream = new BufferedInputStream(stream); AudioInputStream input = AudioSystem.getAudioInputStream(stream); Clip clip = AudioSystem.getClip(); clip.addLineListener(new ClipHandler(sound, chain)); clip.open(input); if(repeatedly) { clip.loop(Clip.LOOP_CONTINUOUSLY); } else { clip.start(); } activeClips.put(sound, clip); } catch (Exception e) { logger.error("Error while playing sound: " + sound, e); } return chain; }
private LineListener addLineListener (final Clip clip, final Object stopMonitor) { final LineListener lineListener = new LineListener () { @Override public void update (final LineEvent event) { final LineEvent.Type type = event.getType (); if (type == LineEvent.Type.STOP) { synchronized (clip) { if (stopMonitor != null) { synchronized (stopMonitor) { stopMonitor.notifyAll (); } } if (event.getFramePosition () != -1) { clip.stop (); clip.close (); clip.notifyAll (); } } } } }; clip.addLineListener (lineListener); return lineListener; }
c.addLineListener( new LineListener()
// ... protected File soundFile; protected Clip clip; protected LineListener listener; public void play() { soundFile = new File(DIR_SOUND + audiotitle); AudioInputStream in = AudioSystem.getAudioInputStream(soundFile); clip = AudioSystem.getClip(); clip.addLineListener(listener); clip.open(in); clip.start(); // ... } // ...
public static void play(String name) { try{ AudioInputStream sounds = AudioSystem.getAudioInputStream(Buzzer.class.getResource(name)); final Clip clip = AudioSystem.getClip(); clip.addLineListener(new LineListener() { public void update(LineEvent e) { LineEvent.Type type = e.getType(); if(type == type.STOP) clip.close(); } }); clip.open(sounds); clip.start(); } catch(Exception e){ e.printStackTrace(); }
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(); } }
clip = AudioSystem.getClip(); clip.open(AudioSystem.getAudioInputStream(new File("go.wav"))); clip.addLineListener(new LineListener() { public void update(LineEvent event) { if(event.getType() == LineEvent.Type.STOP){
/** * @param name The name of the sound file (no extension) */ private static void play(String name) { Preconditions.checkState(allSounds.containsKey(name), "'" + name + "' must be present (did you initialise?)"); byte[] sound = allSounds.get(name); try { final Clip clip = AudioSystem.getClip(); clip.addLineListener(new LineListener() { @Override public void update(LineEvent event) { if (event.getType() == LineEvent.Type.STOP) clip.close(); } }); clip.open(AudioSystem.getAudioInputStream(new ByteArrayInputStream(sound))); clip.start(); } catch (UnsupportedAudioFileException e) { throw new IllegalStateException(e.getMessage()); } catch (IOException e) { throw new IllegalStateException(e.getMessage()); } catch (LineUnavailableException e) { throw new IllegalStateException(e.getMessage()); } }
synchronized void play(InputStream inputStream) throws InterruptedException { // convert the inputstream into an audioinputstream try (BufferedInputStream bis = new BufferedInputStream(inputStream); Clip clip = AudioSystem.getClip(); AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(bis)) { clip.open(audioInputStream); clip.setFramePosition(0); // start at beginning of track clip.addLineListener(this); clip.start(); waitForPlaybackCompletion(); // block until at end of track } catch (Exception e) { throw new RuntimeException(e); } }
private void play(AudioInputStream ais) { try { Clip clip = AudioSystem.getClip(); EndListener listener = new EndListener(); clip.addLineListener(listener); clip.open(ais); playAndWait(clip, listener); } catch (LineUnavailableException | IOException e) { fail(e); } finally { IOUtil.close(ais); } }
clip.open(sound); clip.addLineListener(new LineListener() {
clip.open(sound); clip.addLineListener(new LineListener() {
clip1.addLineListener(listener); clip2.addLineListener(listener); Runnable r = new Runnable() {
clip.addLineListener(new LineListener() { @Override public void update(LineEvent event) {
private void playNext(File file) throws IOException, UnsupportedAudioFileException, LineUnavailableException { synchronized (notificationMutex) { playing = true; } CancelAction cancelAction = new CancelAction(); Application.getInstance().getContext().getNotificationManager().showNotification( MessageFormat.format(Application.getInstance().getContext().getBundle().getString("play-voice-started"), file.getName()), cancelAction); try (AudioInputStream inputStream = AudioSystem.getAudioInputStream(file)) { clip = AudioSystem.getClip(); clip.addLineListener(clipListener); clip.open(inputStream); clip.start(); } }
clip.addLineListener(event -> { if (event.getType() == LineEvent.Type.STOP) { clip.close();
clip.addLineListener(event -> { if (event.getType() == LineEvent.Type.STOP) { clip.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; } } }