Refine search
public static synchronized void playSound(final String url) { new Thread(new Runnable() { // The wrapper thread is unnecessary, unless it blocks on the // Clip finishing; see comments. public void run() { try { Clip clip = AudioSystem.getClip(); AudioInputStream inputStream = AudioSystem.getAudioInputStream( Main.class.getResourceAsStream("/path/to/sounds/" + url)); clip.open(inputStream); clip.start(); } catch (Exception e) { System.err.println(e.getMessage()); } } }).start(); }
/** * 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; }
public static void playSound(File soundfile) throws LineUnavailableException, UnsupportedAudioFileException, IOException{ AudioInputStream audioInputStream = null; audioInputStream = AudioSystem.getAudioInputStream(soundfile); Clip clip = AudioSystem.getClip(); clip.open(audioInputStream); clip.start(); }
InputStream inaudio = getClass().getResourceAsStream("/audio/retrolevel.wav"); Clip clip = null; try { clip = AudioSystem.getClip(); clip.open(AudioSystem.getAudioInputStream(inaudio)); } catch (IOException | LineUnavailableException | UnsupportedAudioFileException e1) { e1.printStackTrace(); } clip.start();