javax.sound.sampled.SourceDataLine#drain ( )源码实例Demo

下面列出了javax.sound.sampled.SourceDataLine#drain ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: MyBox   文件: SoundTools.java
public static void rawplay(AudioFormat targetFormat, AudioInputStream din)
        throws IOException, LineUnavailableException {
    byte[] data = new byte[CommonValues.IOBufferLength];
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, targetFormat);
    SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
    line.open(targetFormat);

    if (line != null) {
        // Start
        FloatControl vol = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
        logger.debug(vol.getValue() + vol.getUnits());
        line.start();
        int nBytesRead = 0, nBytesWritten = 0;
        while (nBytesRead != -1) {
            nBytesRead = din.read(data, 0, data.length);
            if (nBytesRead != -1) {
                nBytesWritten = line.write(data, 0, nBytesRead);
            }
        }
        // Stop
        line.drain();
        line.stop();
        line.close();
        din.close();
    }
}
 
public static void tone(int hz, int msecs, double vol) throws LineUnavailableException {
	byte[] buf = new byte[1];
	AudioFormat af = new AudioFormat(SAMPLE_RATE, // sampleRate
			8, // sampleSizeInBits
			1, // channels
			true, // signed
			false); // bigEndian
	SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
	sdl.open(af);
	sdl.start();
	for (int i = 0; i < msecs * 8; i++) {
		double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
		buf[0] = (byte) (Math.sin(angle) * 127.0 * vol);
		sdl.write(buf, 0, 1);
	}

	sdl.drain();
	sdl.stop();
	sdl.close();
}
 
public static void tone(int hz, int msecs, double vol) throws LineUnavailableException {
	byte[] buf = new byte[1];
	AudioFormat af = new AudioFormat(SAMPLE_RATE, // sampleRate
			8, // sampleSizeInBits
			1, // channels
			true, // signed
			false); // bigEndian
	SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
	sdl.open(af);
	sdl.start();
	for (int i = 0; i < msecs * 8; i++) {
		double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
		buf[0] = (byte) (Math.sin(angle) * 127.0 * vol);
		sdl.write(buf, 0, 1);
	}

	sdl.drain();
	sdl.stop();
	sdl.close();
}
 
源代码4 项目: openjdk-jdk9   文件: bug6372428.java
void playRecorded(AudioFormat format, byte[] data) throws Exception {
    //SourceDataLine line = AudioSystem.getSourceDataLine(format);
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
    SourceDataLine line = (SourceDataLine)AudioSystem.getLine(info);

    line.open();
    line.start();

    int remaining = data.length;
    while (remaining > 0) {
        int avail = line.available();
        if (avail > 0) {
            if (avail > remaining)
                avail = remaining;
            int written = line.write(data, data.length - remaining, avail);
            remaining -= written;
            log("Playing: " + written + " bytes written");
        } else {
            delay(100);
        }
    }

    line.drain();
    line.stop();
}
 
源代码5 项目: DTMF-Decoder   文件: PlayerTest.java
private void rawplay(AudioFormat targetFormat, AudioInputStream din) throws IOException, LineUnavailableException
{
	byte[] data = new byte[4096];
	SourceDataLine line = getLine(targetFormat);		
	if (line != null)
	{
	  // Start
	  line.start();
	  int nBytesRead = 0, nBytesWritten = 0;
	  while (nBytesRead != -1)
	  {
		nBytesRead = din.read(data, 0, data.length);
		if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
	  }
	  // Stop
	  line.drain();
	  line.stop();
	  line.close();
	  din.close();
	}		
}
 
源代码6 项目: DTMF-Decoder   文件: Test.java
private static void rawplay(AudioFormat targetFormat, 
                                   AudioInputStream din) throws IOException, LineUnavailableException
{
   byte[] data = new byte[4096];
  SourceDataLine line = getLine(targetFormat);		
  if (line != null)
  {
     // Start
    line.start();
     int nBytesRead = 0, nBytesWritten = 0;
     while (nBytesRead != -1)
    {
        nBytesRead = din.read(data, 0, data.length);
         if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
    }
     // Stop
    line.drain();
    line.stop();
    line.close();
    din.close();
  }		
}
 
源代码7 项目: google-assistant-java-demo   文件: AudioPlayer.java
public void play(byte[] sound) throws AudioException {
    try {
        AudioFormat format = AudioUtil.getAudioFormat(audioConf);
        DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
        SourceDataLine speakers = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
        speakers.open(format);
        speakers.start();
        speakers.write(sound, 0, sound.length);
        speakers.drain();
        speakers.close();
    } catch (Exception e) {
        throw new AudioException("Unable to play the response", e);
    }
}
 
源代码8 项目: freecol   文件: SoundPlayer.java
/**
 * Play a sound.
 *
 * @param sound The {@code File} to play.
 * @return True if the sound was played without incident.
 * @exception IOException if unable to read or write the sound data.
 */
private boolean playSound(File sound) throws IOException {
    boolean ret = false;
    try (AudioInputStream in = getAudioInputStream(sound)) {
        SourceDataLine line = openLine(in.getFormat(), getMixer(),
                                       data.length);
        if (line == null) return false;
        changeVolume(line, getVolume());
        try {
            this.playDone = false;
            int rd;
            while (!this.playDone && (rd = in.read(data)) > 0) {
                line.write(data, 0, rd);
            }
            ret = true;
            //logger.finest("Played " + sound);
        } finally {
            this.playDone = true;
            line.drain();
            line.stop();
            line.close();
        }
    } catch (IOException ioe) {
        logger.log(Level.WARNING, "Can not open "
            + sound + " as audio stream", ioe);
        return false;
    }
    return ret;
}
 
源代码9 项目: openjdk-jdk9   文件: ChangingBuffer.java
private static boolean doMixerSDL(Mixer mixer, AudioFormat format) {
    if (mixer==null) return false;
    try {
        System.out.println("Trying mixer "+mixer+":");
            DataLine.Info info = new DataLine.Info(
                                      SourceDataLine.class,
                                      format,
                                      (int) samplerate);

            SourceDataLine sdl = (SourceDataLine) mixer.getLine(info);
        System.out.println("  - got sdl: "+sdl);
        System.out.println("  - open with format "+format);
        sdl.open(format);
        System.out.println("  - start...");
        sdl.start();
        System.out.println("  - write...");
        sdl.write(buffer, 0, buffer.length);
        Thread.sleep(200);
        System.out.println("  - drain...");
        sdl.drain();
        System.out.println("  - stop...");
        sdl.stop();
        System.out.println("  - close...");
        sdl.close();
        System.out.println("  - closed");
    } catch (Throwable t) {
        System.out.println("  - Caught exception. Not failed.");
        System.out.println("  - "+t.toString());
        return false;
    }
    return true;
}
 
源代码10 项目: openjdk-jdk9   文件: PlaySine.java
public static int play(boolean shouldPlay) {
    int res = 0;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
    try {
        println("Getting line from mixer...");
        source = (SourceDataLine) mixer.getLine(info);
        println("Opening line...");
        println("  -- if the program is hanging here, kill the process that has blocks the audio device now.");
        source.open(audioFormat);
        println("Starting line...");
        source.start();
        println("Writing audio data for 1 second...");
        long startTime = System.currentTimeMillis();
        while (System.currentTimeMillis() - startTime < 1000) {
            writeData();
            Thread.sleep(100);
        }
        res = 1;
    } catch (IllegalArgumentException iae) {
        println("IllegalArgumentException: "+iae.getMessage());
        println("Sound device cannot handle this audio format.");
        println("ERROR: Test environment not correctly set up.");
        if (source!=null) {
            source.close();
        }
        return 3;
    } catch (LineUnavailableException lue) {
        println("LineUnavailableException: "+lue.getMessage());
        if (shouldPlay) {
            println("ERROR: the line should be available now!.");
            println("       Verify that you killed the other audio process.");
        } else {
            println("Correct behavior! the bug is fixed.");
        }
        res = 2;
    } catch (Exception e) {
        println("Unexpected Exception: "+e.toString());
    }
    if (source != null) {
        println("Draining...");
        try {
            source.drain();
        } catch (NullPointerException npe) {
            println("(NullPointerException: bug fixed in J2SE 1.4.2");
        }
        println("Stopping...");
        source.stop();
        println("Closing...");
        source.close();
        source = null;
    }
    return res;
}
 
源代码11 项目: openjdk-jdk9   文件: TickAtEndOfPlay.java
public static void main(String[] args) throws Exception {
    System.out.println("This test should only be run on Windows.");
    System.out.println("Make sure that the speakers are connected and the volume is up.");
    System.out.println("Close all other programs that may use the soundcard.");

    System.out.println("You'll hear a 2-second tone. when the tone finishes,");
    System.out.println("  there should be no noise. If you hear a short tick/noise,");
    System.out.println("  the bug still applies.");

    System.out.println("Press ENTER to continue.");
    System.in.read();

    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("1")) WorkAround1 = true;
        if (args[i].equals("2")) WorkAround2 = true;
    }
    if (WorkAround1) System.out.println("Using work around1: appending silence");
    if (WorkAround2) System.out.println("Using work around2: waiting before close");

    int zerolen = 0; // how many 0-bytes will be appended to playback
    if (WorkAround1) zerolen = 1000;
    int seconds = 2;
    int sampleRate = 8000;
    double frequency = 1000.0;
    double RAD = 2.0 * Math.PI;
    AudioFormat af = new AudioFormat((float)sampleRate,8,1,true,true);
    System.out.println("Format: "+af);
    DataLine.Info info = new DataLine.Info(SourceDataLine.class,af);
    SourceDataLine source = (SourceDataLine)AudioSystem.getLine(info);
    System.out.println("Line: "+source);
    if (source.toString().indexOf("MixerSourceLine")>=0) {
        System.out.println("This test only applies to non-Java Sound Audio Engine!");
        return;
    }
    System.out.println("Opening...");
    source.open(af);
    System.out.println("Starting...");
    source.start();
    int datalen = sampleRate * seconds;
    byte[] buf = new byte[datalen+zerolen];
    for (int i=0; i<datalen; i++) {
        buf[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);
    }
    System.out.println("Writing...");
    source.write(buf,0,buf.length);
    System.out.println("Draining...");
    source.drain();
    System.out.println("Stopping...");
    source.stop();
    if (WorkAround2) {
        System.out.println("Waiting 200 millis...");
        Thread.sleep(200);
    }
    System.out.println("Closing...");
    source.close();
    System.out.println("Done.");
}