当前位置: 首页>>代码示例>>Java>>正文


Java SourceDataLine.close方法代码示例

本文整理汇总了Java中javax.sound.sampled.SourceDataLine.close方法的典型用法代码示例。如果您正苦于以下问题:Java SourceDataLine.close方法的具体用法?Java SourceDataLine.close怎么用?Java SourceDataLine.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.sound.sampled.SourceDataLine的用法示例。


在下文中一共展示了SourceDataLine.close方法的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: isVisible

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
@Override
public boolean isVisible(JList list, Object value, int row,
		boolean isSelected, boolean cellHasFocus) {
	SoundSource sound = (SoundSource)value;
	SoundUI ui = getSoundUI(sound);
	
	if(!isSelected) {
		SourceDataLine dataLine = ui.line;
		if(dataLine!=null)
			dataLine.close();
	}
	
	boolean visible = isSelected;
	if(!visible) {
		ui.setTargetOpacity(0);
	}
	if(ui.targetOpacity>0) visible = true;
	return visible;
}
 
开发者ID:mickleness,项目名称:pumpernickel,代码行数:20,代码来源:AudioComponentsDemo.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: close

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
/** Close all audio lines. */
public void close() {
	if (exec != null) {
		exec.shutdown();
		for (SourceDataLine sdl : lines) {
			sdl.close();
		}
		try {
			exec.awaitTermination(60, TimeUnit.SECONDS);
		} catch (InterruptedException ex) {
			// ignored
		}
		exec.shutdownNow();
		lines.clear();
		soundPool.clear();
		soundMap.clear();
		soundFormat.clear();
		exec = null;
	}
	
}
 
开发者ID:akarnokd,项目名称:open-ig,代码行数:22,代码来源:Sounds.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,代码来源:PlayerTest.java

示例9: 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

示例10: 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

示例11: closeAllSoundSources

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
private void closeAllSoundSources() {
  if (this.queue.isEmpty()) {
    return;
  }

  while (this.queue.peek() != null) {
    final SourceDataLine clip = this.queue.poll();
    clip.stop();
    clip.flush();
    clip.close();
  }
}
 
开发者ID:gurkenlabs,项目名称:litiengine,代码行数:13,代码来源:SoundSource.java

示例12: 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

示例13: 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

示例14: main

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    if (!isSoundcardInstalled()) {
        return;
    }

    try {
        int COUNT=10;
        out();
        out("4498848 Sound causes crashes on Linux (testing with SourceDataLine)");
        if (args.length>0) {
            COUNT=Integer.parseInt(args[0]);
        }
        for (int i=0; i<COUNT; i++) {
            out("  trial "+(i+1)+"/"+COUNT);
            SourceDataLine sdl = start();
            int waitTime = 500+(1000*(i % 2)); // every 2nd time wait 1500, rather than 500ms.
            out("    waiting for "+waitTime+" ms for audio playback to stop...");
            Thread.sleep(waitTime);
            out("    calling close() from main thread");
            sdl.close();
            // let the subsystem enough time to actually close the soundcard
            out("    waiting for 2 seconds...");
            Thread.sleep(2000);
            out();
        }
        out("  waiting for 1 second...");
        Thread.sleep(1000);
    } catch (Exception e) {
        e.printStackTrace();
        out("  waiting for 1 second");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ie) {}
        // do not fail if no audio device installed - bug 4742021
        if (!(e instanceof LineUnavailableException)) {
            throw e;
        }
    }
    out("Test passed");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:41,代码来源:SDLLinuxCrash.java

示例15: main

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    boolean failed = false;
    try {
        AudioFormat format = new AudioFormat(44100.0f, 16, 2, true, false);
        SourceDataLine sdl = AudioSystem.getSourceDataLine(format);
        try {
            sdl.open(format);
            sdl.start();
            sdl.write(new byte[16384], 0, 16384);
            Thread.sleep(1000);
            int intPos = sdl.getFramePosition();
            long longPos = sdl.getLongFramePosition();
            System.out.println("After 1 second: getFramePosition() = "+intPos);
            System.out.println("            getLongFramePosition() = "+longPos);
            if (intPos <= 0 || longPos <= 0) {
                failed = true;
                System.out.println("## FAILED: frame position did not advance, or negative!");
            }
            if (Math.abs(intPos - longPos) > 100) {
                failed = true;
                System.out.println("## FAILED: frame positions are not the same!");
            }
        } finally {
            sdl.close();
        }
    } catch (LineUnavailableException | IllegalArgumentException e) {
        System.out.println(e);
        System.out.println("Cannot execute test.");
        return;
    }
    if (failed) throw new RuntimeException("Test FAILED!");
    System.out.println("Test Passed.");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:LongFramePosition.java


注:本文中的javax.sound.sampled.SourceDataLine.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。