List of usage examples for javax.sound.sampled Clip start
void start();
From source file:Main.java
public static void main(String[] argv) throws Exception { DataLine.Info info = null;/*from w ww.j a v a2 s. c o m*/ Clip clip = (Clip) AudioSystem.getLine(info); clip.start(); clip.loop(Clip.LOOP_CONTINUOUSLY); int numberOfPlays = 3; clip.loop(numberOfPlays - 1); }
From source file:Main.java
public static void main(String[] argv) throws Exception { AudioInputStream stream = AudioSystem.getAudioInputStream(new File("audiofile")); // From URL//from w w w . j a va2 s . co m // stream = AudioSystem.getAudioInputStream(new URL( // "http://hostname/audiofile")); AudioFormat format = stream.getFormat(); if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) { format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), format.getSampleSizeInBits() * 2, format.getChannels(), format.getFrameSize() * 2, format.getFrameRate(), true); // big endian stream = AudioSystem.getAudioInputStream(format, stream); } DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(), ((int) stream.getFrameLength() * format.getFrameSize())); Clip clip = (Clip) AudioSystem.getLine(info); clip.open(stream); clip.start(); }
From source file:ClipTest.java
public static void main(String[] args) throws Exception { // specify the sound to play // (assuming the sound can be played by the audio system) File soundFile = new File("../sounds/voice.wav"); AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile); // load the sound into memory (a Clip) DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat()); Clip clip = (Clip) AudioSystem.getLine(info); clip.open(sound);//from ww w. j av a2s . c o m // 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(); }
From source file:org.openhab.io.multimedia.actions.Audio.java
private static void playInThread(final Clip clip) { // run in new thread new Thread() { public void run() { try { clip.start(); while (clip.isActive()) { sleep(1000L);//from w ww.j ava2 s. co m } clip.close(); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } }.start(); }
From source file:com.jostrobin.battleships.view.sound.DefaultSoundEffects.java
private void playSound(final String path) { if (enabled) { InputStream is = ClassLoader.getSystemResourceAsStream(path); AudioInputStream audioInputStream = null; try {/*from w w w. j av a 2 s .c o m*/ audioInputStream = AudioSystem.getAudioInputStream(is); Clip clip = AudioSystem.getClip(); clip.open(audioInputStream); clip.start(); while (clip.isRunning()) { Thread.sleep(10); } } catch (Exception e) { logger.warn("Could not play sound effect", e); } finally { IOUtils.closeSilently(is, audioInputStream); } } }
From source file:SimpleSoundPlayer.java
public void playSound() { midiEOM = audioEOM = bump = false;/*from w ww. j a v a2 s .c om*/ if (currentSound instanceof Sequence || currentSound instanceof BufferedInputStream && thread != null) { sequencer.start(); while (!midiEOM && thread != null && !bump) { try { thread.sleep(99); } catch (Exception e) { break; } } sequencer.stop(); sequencer.close(); } else if (currentSound instanceof Clip) { Clip clip = (Clip) currentSound; clip.start(); try { thread.sleep(99); } catch (Exception e) { } while ((paused || clip.isActive()) && thread != null && !bump) { try { thread.sleep(99); } catch (Exception e) { break; } } clip.stop(); clip.close(); } currentSound = null; }
From source file:edu.tsinghua.lumaqq.Sounder.java
/** * /*from ww w.j a v a 2 s .c om*/ */ private void playSound() { // midi? midiEOM = false; if (currentSound instanceof Sequence || currentSound instanceof BufferedInputStream) { /* ?Sequence? */ if (sequencer == null) openSequencer(); sequencer.start(); while (!midiEOM) { try { sleep(99); } catch (Exception e) { break; } } sequencer.stop(); sequencer.close(); } else if (currentSound instanceof Clip) { /* Clip */ Clip clip = (Clip) currentSound; clip.start(); try { sleep(99); } catch (Exception e) { log.error(e.getMessage()); } while (clip.isActive()) { try { sleep(99); } catch (Exception e) { break; } } clip.stop(); clip.close(); } currentSound = null; }
From source file:net.sf.firemox.tools.MToolKit.java
/** * loadClip loads the sound-file into a clip. * /*from w w w. j av a 2 s . c o m*/ * @param soundFile * file to be loaded and played. */ public static void loadClip(String soundFile) { AudioFormat audioFormat = null; AudioInputStream actionIS = null; try { // actionIS = AudioSystem.getAudioInputStream(input); // Does not work ! actionIS = AudioSystem.getAudioInputStream(MToolKit.getFile(MToolKit.getSoundFile(soundFile))); AudioFormat.Encoding targetEncoding = AudioFormat.Encoding.PCM_SIGNED; actionIS = AudioSystem.getAudioInputStream(targetEncoding, actionIS); audioFormat = actionIS.getFormat(); } catch (UnsupportedAudioFileException afex) { Log.error(afex); } catch (IOException ioe) { if (ioe.getMessage().equalsIgnoreCase("mark/reset not supported")) { // Ignore Log.error("IOException ignored."); } Log.error(ioe.getStackTrace()); } // define the required attributes for our line, // and make sure a compatible line is supported. // get the source data line for play back. DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat); if (!AudioSystem.isLineSupported(info)) { Log.error("LineCtrl matching " + info + " not supported."); return; } // Open the source data line for play back. try { Clip clip = null; try { Clip.Info info2 = new Clip.Info(Clip.class, audioFormat); clip = (Clip) AudioSystem.getLine(info2); clip.open(actionIS); clip.start(); } catch (IOException ioe) { Log.error(ioe); } } catch (LineUnavailableException ex) { Log.error("Unable to open the line: " + ex); return; } }
From source file:de.tor.tribes.ui.windows.ClockFrame.java
public synchronized void playSound(String pSound) { Clip clip = null; AudioClip ac = null;// ww w . ja v a2s . c o m try { if (org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS) { clip = AudioSystem.getClip(); BufferedInputStream bin = new BufferedInputStream( ClockFrame.class.getResourceAsStream("/res/" + pSound + ".wav")); AudioInputStream inputStream = AudioSystem.getAudioInputStream(bin); clip.open(inputStream); clip.start(); } else { ac = Applet.newAudioClip(ClockFrame.class.getResource("/res/" + pSound + ".wav")); ac.play(); } } catch (Exception e) { logger.error("Failed to play sound", e); } try { Thread.sleep(2500); } catch (Exception ignored) { } try { if (clip != null) { clip.stop(); clip.flush(); clip = null; } if (ac != null) { ac.stop(); ac = null; } } catch (Exception ignored) { } }
From source file:io.github.jeremgamer.editor.panels.MusicFrame.java
public MusicFrame(JFrame frame, final GeneralSave gs) { ArrayList<BufferedImage> icons = new ArrayList<BufferedImage>(); try {/*from w w w . j a v a2 s .c o m*/ icons.add(ImageIO.read(ImageGetter.class.getResource("icon16.png"))); icons.add(ImageIO.read(ImageGetter.class.getResource("icon32.png"))); icons.add(ImageIO.read(ImageGetter.class.getResource("icon64.png"))); icons.add(ImageIO.read(ImageGetter.class.getResource("icon128.png"))); } catch (IOException e1) { e1.printStackTrace(); } this.setIconImages((List<? extends Image>) icons); this.setTitle("Musique"); this.setSize(new Dimension(300, 225)); this.addWindowListener(new WindowListener() { @Override public void windowActivated(WindowEvent event) { } @Override public void windowClosed(WindowEvent event) { } @Override public void windowClosing(WindowEvent event) { try { gs.save(new File("projects/" + Editor.getProjectName() + "/general.rbd")); } catch (IOException e) { e.printStackTrace(); } if (clip != null) { clip.stop(); clip.close(); try { audioStream.close(); } catch (IOException e) { e.printStackTrace(); } } } @Override public void windowDeactivated(WindowEvent event) { } @Override public void windowDeiconified(WindowEvent event) { } @Override public void windowIconified(WindowEvent event) { } @Override public void windowOpened(WindowEvent event) { } }); this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS)); this.setModal(true); this.setLocationRelativeTo(frame); JPanel properties = new JPanel(); properties.setBorder(BorderFactory.createTitledBorder("Lecture")); ButtonGroup bg = new ButtonGroup(); bg.add(one); bg.add(loop); one.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent event) { JRadioButton rb = (JRadioButton) event.getSource(); if (rb.isSelected()) { gs.set("music.reading", 0); try { gs.save(new File("projects/" + Editor.getProjectName() + "/general.rbd")); } catch (IOException e) { e.printStackTrace(); } if (clip != null) { if (clip.isRunning()) clip.loop(0); } } } }); loop.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent event) { JRadioButton rb = (JRadioButton) event.getSource(); if (rb.isSelected()) { gs.set("music.reading", 1); try { gs.save(new File("projects/" + Editor.getProjectName() + "/general.rbd")); } catch (IOException e) { e.printStackTrace(); } if (clip != null) { if (clip.isRunning()) clip.loop(Clip.LOOP_CONTINUOUSLY); } } } }); properties.add(one); properties.add(loop); if (gs.getInt("music.reading") == 0) { one.setSelected(true); } else { loop.setSelected(true); } volume.setMaximum(100); volume.setMinimum(0); volume.setValue(30); volume.setPaintTicks(true); volume.setPaintLabels(true); volume.setMinorTickSpacing(10); volume.setMajorTickSpacing(20); volume.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent event) { JSlider slider = (JSlider) event.getSource(); double value = slider.getValue(); gain = value / 100; dB = (float) (Math.log(gain) / Math.log(10.0) * 20.0); if (clip != null) gainControl.setValue(dB); gs.set("music.volume", (int) value); } }); volume.setValue(gs.getInt("music.volume")); properties.add(volume); properties.setPreferredSize(new Dimension(300, 125)); content.add(properties); JPanel browsePanel = new JPanel(); browsePanel.setBorder(BorderFactory.createTitledBorder("")); JButton browse = new JButton("Parcourir..."); if (new File("projects/" + Editor.getProjectName() + "/music.wav").exists()) { preview.setEnabled(false); browse.setText(""); try { browse.setIcon(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png")))); } catch (IOException e) { e.printStackTrace(); } } browse.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { JButton button = (JButton) event.getSource(); if (new File("projects/" + Editor.getProjectName() + "/music.wav").exists()) { if (clip != null) { clip.stop(); clip.close(); try { audioStream.close(); } catch (IOException e) { e.printStackTrace(); } } name.setText(""); preview.setEnabled(false); button.setText("Parcourir..."); button.setIcon(null); new File("projects/" + Editor.getProjectName() + "/music.wav").delete(); gs.set("music.name", ""); } else { String path = null; JFileChooser chooser = new JFileChooser(Editor.lastPath); FileNameExtensionFilter filter = new FileNameExtensionFilter("Audio (WAV)", "wav"); chooser.setFileFilter(filter); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int option = chooser.showOpenDialog(null); if (option == JFileChooser.APPROVE_OPTION) { path = chooser.getSelectedFile().getAbsolutePath(); Editor.lastPath = chooser.getSelectedFile().getParent(); copyMusic(new File(path)); button.setText(""); try { button.setIcon( new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png")))); } catch (IOException e) { e.printStackTrace(); } gs.set("music.name", new File(path).getName()); try { gs.save(new File("projects/" + Editor.getProjectName() + "/general.rbd")); } catch (IOException e) { e.printStackTrace(); } name.setText(new File(path).getName()); preview.setEnabled(true); } } } }); if (new File("projects/" + Editor.getProjectName() + "/music.wav").exists()) { preview.setEnabled(true); } else { preview.setEnabled(false); } preview.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { JToggleButton tb = (JToggleButton) event.getSource(); if (tb.isSelected()) { try { audioStream = AudioSystem.getAudioInputStream( new File("projects/" + Editor.getProjectName() + "/music.wav")); format = audioStream.getFormat(); info = new DataLine.Info(Clip.class, format); clip = (Clip) AudioSystem.getLine(info); clip.open(audioStream); clip.start(); gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); gainControl.setValue(dB); if (loop.isSelected()) { clip.loop(Clip.LOOP_CONTINUOUSLY); } else { clip.loop(0); } clip.addLineListener(new LineListener() { @Override public void update(LineEvent event) { Clip clip = (Clip) event.getSource(); if (!clip.isRunning()) { preview.setSelected(false); clip.stop(); clip.close(); try { audioStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }); } catch (Exception exc) { exc.printStackTrace(); } } else { clip.stop(); clip.close(); try { audioStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }); JPanel buttons = new JPanel(); buttons.setLayout(new BorderLayout()); buttons.add(browse, BorderLayout.WEST); buttons.add(preview, BorderLayout.EAST); browsePanel.setLayout(new BorderLayout()); browsePanel.add(buttons, BorderLayout.NORTH); browsePanel.add(name, BorderLayout.SOUTH); name.setPreferredSize(new Dimension(280, 25)); name.setText(gs.getString("music.name")); content.add(browsePanel); this.setContentPane(content); this.setVisible(true); }