List of usage examples for javax.sound.sampled SourceDataLine open
void open(AudioFormat format, int bufferSize) throws LineUnavailableException;
From source file:SoundManagerTest.java
/** * Signals that a PooledThread has started. Creates the Thread's line and * buffer.//from w ww . j a v a 2s. c o m */ protected void threadStarted() { // wait for the SoundManager constructor to finish synchronized (this) { try { wait(); } catch (InterruptedException ex) { } } // use a short, 100ms (1/10th sec) buffer for filters that // change in real-time int bufferSize = playbackFormat.getFrameSize() * Math.round(playbackFormat.getSampleRate() / 10); // create, open, and start the line SourceDataLine line; DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, playbackFormat); try { line = (SourceDataLine) AudioSystem.getLine(lineInfo); line.open(playbackFormat, bufferSize); } catch (LineUnavailableException ex) { // the line is unavailable - signal to end this thread Thread.currentThread().interrupt(); return; } line.start(); // create the buffer byte[] buffer = new byte[bufferSize]; // set this thread's locals localLine.set(line); localBuffer.set(buffer); }
From source file:Filter3dTest.java
/** * Plays a stream. This method blocks (doesn't return) until the sound is * finished playing.// ww w . j a v a 2 s . com */ public void play(InputStream source) { // use a short, 100ms (1/10th sec) buffer for real-time // change to the sound stream int bufferSize = format.getFrameSize() * Math.round(format.getSampleRate() / 10); byte[] buffer = new byte[bufferSize]; // create a line to play to SourceDataLine line; try { DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); line = (SourceDataLine) AudioSystem.getLine(info); line.open(format, bufferSize); } catch (LineUnavailableException ex) { ex.printStackTrace(); return; } // start the line line.start(); // copy data to the line try { int numBytesRead = 0; while (numBytesRead != -1) { numBytesRead = source.read(buffer, 0, buffer.length); if (numBytesRead != -1) { line.write(buffer, 0, numBytesRead); } } } catch (IOException ex) { ex.printStackTrace(); } // wait until all data is played, then close the line line.drain(); line.close(); }