Refine search
/** * Loops an audio file (in .wav, .mid, or .au format) in a background thread. * * @param filename the name of the audio file * @throws IllegalArgumentException if {@code filename} is {@code null} */ public static synchronized void loop(String filename) { if (filename == null) throw new IllegalArgumentException(); // code adapted from: http://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java try { Clip clip = AudioSystem.getClip(); InputStream is = StdAudio.class.getResourceAsStream(filename); AudioInputStream ais = AudioSystem.getAudioInputStream(is); clip.open(ais); clip.loop(Clip.LOOP_CONTINUOUSLY); } catch (UnsupportedAudioFileException e) { throw new IllegalArgumentException("unsupported audio format: '" + filename + "'", e); } catch (LineUnavailableException e) { throw new IllegalArgumentException("could not play '" + filename + "'", e); } catch (IOException e) { throw new IllegalArgumentException("could not play '" + filename + "'", e); } }
/** * 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); } }
public void actionPerformed(ActionEvent e) { synchronized (clip) { if (clip.isActive()) { System.err.println("Stopping clip."); clip.stop(); } else { System.err.println("Rewinding clip."); if (Double.isNaN(positionCursor.x)) { // no cursor, play from start clip.setFramePosition(0); } else { // play from cursor position clip.setFramePosition(X2indexX(positionCursor.x)); } if (!Double.isNaN(rangeCursor.x)) { // range set? System.err.println("Setting timer task"); int endFrame = X2indexX(rangeCursor.x); timer.schedule(new ClipObserver(clip, endFrame), 50, 50); } System.err.println("Starting clip."); clip.start(); } } } });
public void run() { System.err.println("Timer task running"); if (!clip.isActive() // already stopped? || clip.getFramePosition() >= endFrame) { System.err.println("Timer task stopping clip."); clip.stop(); this.cancel(); } } }
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(); }
URL url = new URL("foo.wav"); Clip clip = AudioSystem.getClip(); AudioInputStream ais = AudioSystem.getAudioInputStream(url); clip.open(ais); System.out.println(clip.getMicrosecondLength());
/** * Creates new AudioClip. If the clip cannot be opened no * exception is thrown, instead the methods of the AudioClip * return without performing any operations. * * @param location the clip location */ public AppletAudioClip(URL location) { try { AudioInputStream stream = AudioSystem.getAudioInputStream(location); clip = (Clip) AudioSystem.getLine(new Line.Info(Clip.class)); clip.open(stream); } catch (Exception ex) { System.err.println("Error loading sound:"+location.toString()); clip = null; } }
File file = new File(JavaApplication.class.getResource("1.wav").getPath()); AudioInputStream inputStream1 = AudioSystem.getAudioInputStream(file); AudioInputStream inputStream2 = AudioSystem.getAudioInputStream(file); Clip clip = AudioSystem.getClip(); clip.open(inputStream1); clip.setFramePosition(0); clip.start(); Clip clip2 = AudioSystem.getClip(); clip2.open(inputStream2); clip2.setFramePosition(0); clip2.start();
URL defaultSound = getClass().getResource("/folder/folder/audio.wav"); File soundFile = new File(defaultSound.toURI()); System.out.println("defaultSound " + defaultSound); // check the URL! AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(defaultSound); Clip clip = AudioSystem.getClip(); clip.open(audioInputStream); clip.start( );
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setTitle("Test Sound"); f.setSize(300, 200); f.setVisible(false); AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile); Clip clip = AudioSystem.getClip(); clip.open(audioIn); clip.start(); clips.add(clip); } catch (UnsupportedAudioFileException e) { clips.get(id-1).stop(); return true;
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 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; } } }
/** constructor */ public WaveFile(String path) { try { File file=new File(path); javax.sound.sampled.AudioFileFormat audioFileFormat=javax.sound.sampled.AudioSystem.getAudioFileFormat(file); m_stream=javax.sound.sampled.AudioSystem.getAudioInputStream(file); javax.sound.sampled.AudioFormat format=m_stream.getFormat(); javax.sound.sampled.DataLine.Info info=new javax.sound.sampled.DataLine.Info(javax.sound.sampled.Clip.class,format,((int)m_stream.getFrameLength()*format.getFrameSize())); m_clip=(javax.sound.sampled.Clip)javax.sound.sampled.AudioSystem.getLine(info); m_clip.open(m_stream); } catch(Exception e) { e.printStackTrace(); } }
audioInputStream = AudioSystem.getAudioInputStream(clipFile); } catch (Exception e) { e.printStackTrace(); AudioFormat format = audioInputStream.getFormat(); DataLine.Info info = new DataLine.Info(Clip.class, format); try { m_clip = (Clip) AudioSystem.getLine(info); m_clip.open(audioInputStream); } catch (LineUnavailableException e) { e.printStackTrace(); e.printStackTrace(); m_clip.loop(loop); if (waitUntilCompleted) m_clip.drain(); } else { System.out.println("playWavFile<init>(): can't get data from file " + clipFile.getName());
this.pack(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); try { AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url); clip = AudioSystem.getClip(); clip.open(audioInputStream); } catch (UnsupportedAudioFileException e) { e.printStackTrace(); if (clip.isRunning()) clip.stop(); // Stop the player if it is still running clip.setFramePosition(0); // rewind to the beginning clip.start(); // Start playing
try { File file = new File("audio.wav"); Clip clip = AudioSystem.getClip(); clip.open(AudioSystem.getAudioInputStream(file)); clip.loop(Clip.LOOP_CONTINUOUSLY); clip.start(); } catch (Exception e) { System.err.println("Put the music.wav file in the sound folder if you want to play background music, only optional!"); JLabel l = new JLabel(); ImageIcon icon = new ImageIcon(arg); f.setSize(480, 360); f.setVisible(true); l.setIcon(icon); p.add(l); f.getContentPane().add(p); f.setLocationRelativeTo(null); f.setResizable(false); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
stream = AudioSystem.getAudioInputStream(soundFile); format = stream.getFormat(); info = new DataLine.Info(Clip.class, format); clip = (Clip) AudioSystem.getLine(info); clip.open(stream); clip.start(); while(clip.getMicrosecondLength() != clip.getMicrosecondPosition()) {} clip.close(); stream.close(); soundFile.close(); }catch(Exception ex){
private void loadAndPlayAudio(String audioResource) { try (AudioInputStream soundStream = AudioSystem.getAudioInputStream(MainPanel.class.getResource(audioResource))) { DataLine.Info info = new DataLine.Info(Clip.class, soundStream.getFormat()); Clip clip = (Clip) AudioSystem.getLine(info); clip.open(soundStream); clip.start(); } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) { ex.printStackTrace(); } } // import java.security.*;