本文整理汇总了Java中javax.sound.sampled.AudioSystem.getSourceDataLine方法的典型用法代码示例。如果您正苦于以下问题:Java AudioSystem.getSourceDataLine方法的具体用法?Java AudioSystem.getSourceDataLine怎么用?Java AudioSystem.getSourceDataLine使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.sound.sampled.AudioSystem
的用法示例。
在下文中一共展示了AudioSystem.getSourceDataLine方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: restartSDL
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
public void restartSDL(){
AudioFormat form = new AudioFormat(sys.getSampleRate(),16,2,true,false);
bufptr=0;
audioints = new int[(int)((sys.getSampleRate()/1000.0)*sys.getBufferSize())*2];
if(scope!=null)
scope.setAudio(audioints);
audiobuffer = new byte[audioints.length*2];
try {
if(sdl!=null)
sdl.close();
sdl = AudioSystem.getSourceDataLine(form);
sdl.open(form,audiobuffer.length*3);
sdl.start();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
示例2: start
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
@Override
public void start() {
if (line != null) {
LOG.debug("Sound already started");
return;
}
LOG.debug("Start sound");
try {
line = AudioSystem.getSourceDataLine(FORMAT);
line.open(FORMAT, BUFFER_SIZE);
} catch (LineUnavailableException e) {
throw new RuntimeException(e);
}
line.start();
buffer = new byte[line.getBufferSize()];
divider = (int) (Gameboy.TICKS_PER_SEC / FORMAT.getSampleRate());
}
示例3: playGradient
import javax.sound.sampled.AudioSystem; //导入方法依赖的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();
}
}
示例4: playSound
import javax.sound.sampled.AudioSystem; //导入方法依赖的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();
}
}
示例5: isSoundcardInstalled
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
/**
* Returns true if at least one soundcard is correctly installed
* on the system.
*/
public static boolean isSoundcardInstalled() {
boolean result = false;
try {
Mixer.Info[] mixers = AudioSystem.getMixerInfo();
if (mixers.length > 0) {
result = AudioSystem.getSourceDataLine(null) != null;
}
} catch (Exception e) {
System.err.println("Exception occured: "+e);
}
if (!result) {
System.err.println("Soundcard does not exist or sound drivers not installed!");
System.err.println("This test requires sound drivers for execution.");
}
return result;
}
示例6: isSoundcardInstalled
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
/**
* Returns true if at least one soundcard is correctly installed
* on the system.
*/
public static boolean isSoundcardInstalled() {
boolean result = false;
try {
Mixer.Info[] mixers = AudioSystem.getMixerInfo();
if (mixers.length > 0) {
result = AudioSystem.getSourceDataLine(null) != null;
}
} catch (Exception e) {
System.err.println("Exception occured: " + e);
}
if (!result) {
System.err.println(
"Soundcard does not exist or sound drivers not installed!");
System.err.println(
"This test requires sound drivers for execution.");
}
return result;
}
示例7: isSoundcardInstalled
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
/**
* Returns true if at least one soundcard is correctly installed
* on the system.
*/
public static boolean isSoundcardInstalled() {
boolean result = false;
try {
Mixer.Info[] mixers = AudioSystem.getMixerInfo();
if (mixers.length > 0) {
result = AudioSystem.getSourceDataLine(null) != null;
}
} catch (Exception e) {
System.err.println("Exception occured: "+e);
}
if (!result) {
System.err.println("Soundcard does not exist or sound drivers not installed!");
System.err.println("This test requires sound drivers for execution.");
}
return result;
}
示例8: AudioPlayer
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
public AudioPlayer(InputStream in, Listener listener) throws Exception {
this.in = in;
this.listener = listener;
AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 2, 4, 44100.0F, true);
line = AudioSystem.getSourceDataLine(format);
line.open(format);
LOG.debug("Opened line " + line);
if (line.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
gainControl = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
setGain(DEFAULT_GAIN);
}
new AudioDataWriter();
}
示例9: playSounds
import javax.sound.sampled.AudioSystem; //导入方法依赖的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();
}
}
示例10: main
import javax.sound.sampled.AudioSystem; //导入方法依赖的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.");
}