List of usage examples for javax.sound.sampled SourceDataLine stop
void stop();
From source file:Main.java
public static void main(String[] argv) throws Exception { AudioInputStream stream = AudioSystem.getAudioInputStream(new File("audiofile")); // 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); }// w w w .j a v a 2 s . c om SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, stream.getFormat(), ((int) stream.getFrameLength() * format.getFrameSize())); SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); line.open(stream.getFormat()); line.start(); int numRead = 0; byte[] buf = new byte[line.getBufferSize()]; while ((numRead = stream.read(buf, 0, buf.length)) >= 0) { int offset = 0; while (offset < numRead) { offset += line.write(buf, offset, numRead - offset); } } line.drain(); line.stop(); }
From source file:tvbrowser.extras.reminderplugin.ReminderPlugin.java
/** * Plays a sound./*ww w.ja v a 2 s . c om*/ * * @param fileName * The file name of the sound to play. * @return The sound Object. */ public static Object playSound(final String fileName) { try { if (StringUtils.endsWithIgnoreCase(fileName, ".mid")) { final Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); final InputStream midiFile = new FileInputStream(fileName); sequencer.setSequence(MidiSystem.getSequence(midiFile)); sequencer.start(); new Thread("Reminder MIDI sequencer") { @Override public void run() { setPriority(Thread.MIN_PRIORITY); while (sequencer.isRunning()) { try { Thread.sleep(100); } catch (Exception ee) { // ignore } } try { sequencer.close(); midiFile.close(); } catch (Exception ee) { // ignore } } }.start(); return sequencer; } else { final AudioInputStream ais = AudioSystem.getAudioInputStream(new File(fileName)); final AudioFormat format = ais.getFormat(); final DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); if (AudioSystem.isLineSupported(info)) { final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); line.open(format); line.start(); new Thread("Reminder audio playing") { private boolean stopped; @Override public void run() { byte[] myData = new byte[1024 * format.getFrameSize()]; int numBytesToRead = myData.length; int numBytesRead = 0; int total = 0; int totalToRead = (int) (format.getFrameSize() * ais.getFrameLength()); stopped = false; line.addLineListener(new LineListener() { public void update(LineEvent event) { if (line != null && !line.isRunning()) { stopped = true; line.close(); try { ais.close(); } catch (Exception ee) { // ignore } } } }); try { while (total < totalToRead && !stopped) { numBytesRead = ais.read(myData, 0, numBytesToRead); if (numBytesRead == -1) { break; } total += numBytesRead; line.write(myData, 0, numBytesRead); } } catch (Exception e) { } line.drain(); line.stop(); } }.start(); return line; } else { URL url = new File(fileName).toURI().toURL(); AudioClip clip = Applet.newAudioClip(url); clip.play(); } } } catch (Exception e) { e.printStackTrace(); if ((new File(fileName)).isFile()) { URL url; try { url = new File(fileName).toURI().toURL(); AudioClip clip = Applet.newAudioClip(url); clip.play(); } catch (MalformedURLException e1) { } } else { String msg = mLocalizer.msg("error.1", "Error loading reminder sound file!\n({0})", fileName); JOptionPane.showMessageDialog(UiUtilities.getBestDialogParent(MainFrame.getInstance()), msg, Localizer.getLocalization(Localizer.I18N_ERROR), JOptionPane.ERROR_MESSAGE); } } return null; }