當前位置: 首頁>>代碼示例>>Java>>正文


Java SourceDataLine.drain方法代碼示例

本文整理匯總了Java中javax.sound.sampled.SourceDataLine.drain方法的典型用法代碼示例。如果您正苦於以下問題:Java SourceDataLine.drain方法的具體用法?Java SourceDataLine.drain怎麽用?Java SourceDataLine.drain使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.sound.sampled.SourceDataLine的用法示例。


在下文中一共展示了SourceDataLine.drain方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: playGradient

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
public static void playGradient(double fstart,double fend,double duration,double volume,byte fadeend,byte wave) {
	byte[] freqdata = new byte[(int)(duration * SAMPLE_RATE)];
	
	// Generate the sound
	for(int i = 0; i < freqdata.length; i++) {
		freqdata[i] = (byte)generateValue(i, duration, fstart + (fend-fstart) * (i/(double)freqdata.length), volume, fadeend, wave);
	}
		
	// Play it
	try {
		final AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
		SourceDataLine line = AudioSystem.getSourceDataLine(af);
		line.open(af, SAMPLE_RATE);
		line.start();
		line.write(freqdata, 0, freqdata.length);
	    line.drain();
	    line.close();
	}catch(LineUnavailableException e) {
		e.printStackTrace();
	}
}
 
開發者ID:vanyle,項目名稱:Explorium,代碼行數:22,代碼來源:SoundGenerator.java

示例2: playSound

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
/**
 * Play a sound at a given frequency freq during duration (in seconds) with volume as strenght
 * <br/><br/>
 * <code>SoundGenerator.playSound(440.0,1.0,0.5,SoundGenerator.FADE_LINEAR,SoundGenerator.WAVE_SIN);</code><br/>
 * Available fades : FADE_NONE, FADE_LINEAR, FADE_QUADRATIC<br/>
 * Available waves : WAVE_SIN, WAVE_SQUARE, WAVE_TRIANGLE, WAVE_SAWTOOTH<br/>
 */
public static void playSound(double freq,double duration,double volume,byte fade,byte wave){
	
	double[] soundData = generateSoundData(freq,duration,volume,fade,wave);
	byte[] freqdata = new byte[soundData.length];
	
	for(int i = 0;i < soundData.length;i++) {
		freqdata[i] = (byte)soundData[i];
	}
		
	// Play it
	try {
		final AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
		SourceDataLine line = AudioSystem.getSourceDataLine(af);
		line.open(af, SAMPLE_RATE);
		line.start();
		line.write(freqdata, 0, freqdata.length);
	    line.drain();
	    line.close();
	}catch(LineUnavailableException e) {
		e.printStackTrace();
	}
}
 
開發者ID:vanyle,項目名稱:Explorium,代碼行數:30,代碼來源:SoundGenerator.java

示例3: playSound

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
/**
 * Play a sound.
 *
 * @param in The {@code AudioInputStream} to play.
 * @return True if the stream was played without incident.
 * @exception IOException if unable to read or write the sound data.
 */
private boolean playSound(AudioInputStream in) throws IOException {
    boolean ret = false;

    SourceDataLine line = openLine(in.getFormat());
    if (line == null) return false;
    try {
        startPlaying();
        int rd;
        while (keepPlaying() && (rd = in.read(data)) > 0) {
            line.write(data, 0, rd);
        }
        ret = true;
    } finally {
        stopPlaying();
        line.drain();
        line.stop();
        line.close();
    }
    return ret;
}
 
開發者ID:FreeCol,項目名稱:freecol,代碼行數:28,代碼來源:SoundPlayer.java

示例4: playRecorded

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
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();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:26,代碼來源:bug6372428.java

示例5: rawplay

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
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();
  } 
}
 
開發者ID:debmalya,項目名稱:JavaVAD,代碼行數:22,代碼來源:MP3Player.java

示例6: reproduce

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
private static void reproduce( byte soundbytes[]) {
	try {
		DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, AudioFormatHelper.getAudioFormat());
		
		// El source data line se usa para escribir datos en el
		SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
		sourceDataLine.open(AudioFormatHelper.getAudioFormat());
		sourceDataLine.start();
		
		sourceDataLine.write(soundbytes, 0, soundbytes.length);
		sourceDataLine.drain();
		sourceDataLine.close();
	} catch (Exception e) {
		// Log and Handle exception
		e.printStackTrace();
	}
}
 
開發者ID:ldebello,項目名稱:javacuriosities,代碼行數:18,代碼來源:AudioReceiver.java

示例7: rawplay

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
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();
	}		
}
 
開發者ID:fredsa,項目名稱:forplay,代碼行數:22,代碼來源:PlayerTest.java

示例8: rawplay

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
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();
    }       
}
 
開發者ID:fredsa,項目名稱:forplay,代碼行數:22,代碼來源:SkipTest.java

示例9: rawplay

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
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();
  }		
}
 
開發者ID:tino1b2be,項目名稱:DTMF-Decoder,代碼行數:23,代碼來源:Test.java

示例10: playSounds

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
/**
 * Same as playSound but plays several frequences at the same time<br/>
 * <code>
 * double[] freqs = {440.0,440*1.5}; <br/>
 * SoundGenerator.playSound(freqs,1.0,0.5,SoundGenerator.FADE_LINEAR,SoundGenerator.WAVE_SIN);
 * </code>
 * 
 */
public static void playSounds(double[] freqs,double duration,double volume,byte fade,byte wave) {
	if(freqs.length == 0) {
		System.err.println("No frequences to play !");
		return;
	}
	volume = volume / freqs.length; // ease volume to avoid overflow
	
	double[][] soundData = new double[freqs.length][];
	
	for(int i = 0;i < soundData.length;i++) {
		soundData[i] = generateSoundData(freqs[i],duration,volume,fade,wave);
	}
	byte[] freqdata = new byte[soundData[0].length];
	
	for(int i = 0;i < soundData[0].length;i++) {
		for(int j = 0;j < soundData.length;j++) {
			freqdata[i] += (byte)(soundData[j][i]);
		}
	}
		
	// Play it
	try {
		final AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
		SourceDataLine line = AudioSystem.getSourceDataLine(af);
		line.open(af, SAMPLE_RATE);
		line.start();
		line.write(freqdata, 0, freqdata.length);
	    line.drain();
	    line.close();
	}catch(LineUnavailableException e) {
		e.printStackTrace();
	}
}
 
開發者ID:vanyle,項目名稱:Explorium,代碼行數:42,代碼來源:SoundGenerator.java

示例11: play

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
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);
    }
}
 
開發者ID:mautini,項目名稱:google-assistant-java-demo,代碼行數:15,代碼來源:AudioPlayer.java

示例12: doMixerSDL

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
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;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:33,代碼來源:ChangingBuffer.java

示例13: playSound

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
public static void playSound(InputStream is) throws LineUnavailableException, UnsupportedAudioFileException, IOException {
    AudioInputStream audioStream = AudioSystem.getAudioInputStream(is);

    AudioFormat audioFormat = audioStream.getFormat();

    DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
    //jdk6
    SourceDataLine sourceLine = (SourceDataLine) AudioSystem.getLine(info);
    try {
        sourceLine.open(audioFormat);

        sourceLine.start();

        int nBytesRead = 0;
        byte[] abData = new byte[BUFFER_SIZE];
        while (nBytesRead != -1) {
            nBytesRead = audioStream.read(abData, 0, abData.length);

            if (nBytesRead >= 0) {
                int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
            }
        }

        sourceLine.drain();
    } finally {
        sourceLine.close();
    }
}
 
開發者ID:judovana,項目名稱:FlashBalknaSwing,代碼行數:29,代碼來源:WavPlayerImpl.java

示例14: stream

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
/**
* Small sections of audio bytes are read off, watching for a call to stop, pause, restart, or mute the audio.
* @param targetFormat Format the audio will be changed to.
* @param din The audio stream.
*/
private void stream(AudioFormat targetFormat, AudioInputStream din)
{
	try {
		byte[] data = new byte[byteChunkSize];
		SourceDataLine line = getLine(targetFormat);
		if (line != null) 
		{
			line.start();
			int nBytesRead = 0;
			while(nBytesRead != -1 && running && !restart && !isStopped) 
			{
				nBytesRead = din.read(data, 0, data.length);
				if(nBytesRead != -1)
				{
					if (mute)
						line.write(muteData, 0, nBytesRead);
					else
						line.write(data, 0, nBytesRead);
				
				}
				while(pause && running)
					wait(15);
			}
			line.drain();
			line.stop();
			line.close();
			din.close();
		}
	} catch(Exception e) {
		System.err.println("Problem playing audio!");
		e.printStackTrace();
	}
}
 
開發者ID:Sw4T,項目名稱:GrooveBerry,代碼行數:39,代碼來源:AudioFile.java

示例15: rawplay

import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
private void rawplay(AudioFormat targetFormat, AudioInputStream dataIn) throws IOException, LineUnavailableException {
    // get buffer
    byte[] data = new byte[4096];

    // get line
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, targetFormat);
    SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);

    if (line != null) {
        // open it
        line.open();

        // start
        line.start();
        int nBytesRead = 0, nBytesWritten = 0;
        while (nBytesRead != -1) {
            nBytesRead = dataIn.read(data, 0, data.length);
            if (nBytesRead != -1) {
                nBytesWritten = line.write(data, 0, nBytesRead);
            }
        }

        // stop line
        line.drain();
        line.stop();
        line.close();

        // stop input stream
        dataIn.close();
    }
}
 
開發者ID:Trilarion,項目名稱:java-vorbis-support,代碼行數:32,代碼來源:SPIDecoderTest.java


注:本文中的javax.sound.sampled.SourceDataLine.drain方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。