本文整理匯總了Java中javax.sound.sampled.SourceDataLine.getControl方法的典型用法代碼示例。如果您正苦於以下問題:Java SourceDataLine.getControl方法的具體用法?Java SourceDataLine.getControl怎麽用?Java SourceDataLine.getControl使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.sound.sampled.SourceDataLine
的用法示例。
在下文中一共展示了SourceDataLine.getControl方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: StreamingPlayback
import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
/**
* Creates a new {@code StreamingPlayback}. StreamingPlayback objects will
* always be created by their associated StreamingAudio object.
*
* @param audio
* The {@code Audio} that created this {@code StreamingPlayback}.
* @param audioInStream
* The {@code AudioInputStream} used by this
* {@code StreamingPlayback}.
* @param instanceID
* The {@code instanceID} of this {@code StreamingPlayback}.
*/
protected StreamingPlayback(Audio audio, AudioInputStream audioInStream,
long instanceID) {
super(audio, instanceID);
this.audioInStream = audioInStream;
AudioFormat audioFormat = audioInStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
audioFormat);
try {
line = (SourceDataLine) AudioSystem.getLine(info);
if (line != null) {
line.open(audioFormat);
}
if (line.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
volCtrl = (FloatControl) line
.getControl(FloatControl.Type.MASTER_GAIN);
} else {
logger.warning("Master-Gain control is not supported."
+ " Volume will be fixed at the default level.");
}
} catch (LineUnavailableException ex) {
ex.printStackTrace();
}
}
示例2: initSound
import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
private void initSound() {
if (quiet == null) {
quiet = new byte[WAVE_LEN];
}
if (buffer == null) {
byte[] buf = new byte[WAVE_LEN];
double f1 = 0;
for (int i = 0, n = WAVE_LEN; i < n; i++) {
f1 = Math.sin(i * 3.141592 * 2 / WAVE_LEN) * 40;
f1 += Math.sin(i * 3.141592 * 4 / WAVE_LEN) * 30;
buf[i] = (byte) (f1);
}
buffer = buf;
}
if (soundEvent == null) {
soundEvent = new TimeEvent(0, "Beeper") {
public void execute(long t) {
if (isSoundEnabled) {
ioTick(t);
cpu.scheduleCycleEvent(this, cpu.cycles + 1000);
}
}
};
}
AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, false);
DataLine.Info dli = new DataLine.Info(SourceDataLine.class, af, 16384);
try {
dataLine = (SourceDataLine) AudioSystem.getLine(dli);
if (dataLine == null) {
logw(WarningType.EMULATION_ERROR, "No audio data line available");
} else {
dataLine.open(dataLine.getFormat(), 16384);
volume = (FloatControl) dataLine.getControl(FloatControl.Type.MASTER_GAIN);
}
} catch (Exception e) {
logw(WarningType.EMULATION_ERROR, "Could not get audio data line: " + e);
}
if (dataLine != null) {
isSoundEnabled = true;
dataLine.start();
}
}
示例3: playAudioFile
import javax.sound.sampled.SourceDataLine; //導入方法依賴的package包/類
public static void playAudioFile(File file, int playDurationMillis)
{
try
{
// Get AudioInputStream from given file.
AudioInputStream compressedInput = AudioSystem.getAudioInputStream(file);
AudioInputStream decodedInput = null;
if (compressedInput != null)
{
AudioFormat baseFormat = compressedInput.getFormat();
AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16,
baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
// Get AudioInputStream that will be decoded by underlying VorbisSPI
decodedInput = AudioSystem.getAudioInputStream(format, compressedInput);
int oneSecondBytes = (int) Math.ceil(format.getFrameSize() * format.getFrameRate());
int fadeOutDurationBytes = oneSecondBytes / 20;
int playDurationBytes;
if (playDurationMillis <= 0)
playDurationBytes = Integer.MAX_VALUE;
else
playDurationBytes = oneSecondBytes * playDurationMillis / 1000;
byte[] data = new byte[1024];
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(new DataLine.Info(SourceDataLine.class,
format));
line.open(format);
FloatControl gainControl = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
float initialGain = gainControl.getValue();
// Start
line.start();
int bytesRead;
int totalBytes = 0;
while ((bytesRead = decodedInput.read(data, 0, data.length)) != -1)
{
totalBytes += bytesRead;
int playedBytes = totalBytes;
if (totalBytes >= playDurationBytes)
{
bytesRead -= totalBytes - playDurationBytes;
playedBytes = playDurationBytes;
}
if (playDurationBytes - playedBytes < fadeOutDurationBytes)
{
float fadePct = ((float) (playDurationBytes - playedBytes)) / fadeOutDurationBytes;
gainControl.setValue(initialGain * fadePct + gainControl.getMinimum() * (1.0f - fadePct));
}
line.write(data, 0, bytesRead);
if (totalBytes >= playDurationBytes)
break;
}
// Stop
line.drain();
gainControl.setValue(initialGain);
line.stop();
line.close();
decodedInput.close();
compressedInput.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}