本文整理汇总了Java中javax.sound.sampled.AudioSystem.getLine方法的典型用法代码示例。如果您正苦于以下问题:Java AudioSystem.getLine方法的具体用法?Java AudioSystem.getLine怎么用?Java AudioSystem.getLine使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.sound.sampled.AudioSystem
的用法示例。
在下文中一共展示了AudioSystem.getLine方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: VirtualDrummerMicrophoneInput
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
/** Creates a new instance of test. Opens the microphone input as the target line.
* To start the reporting, {@link #start} the thread.
* @throws LineUnavailableException if microphone input is not available
*/
public VirtualDrummerMicrophoneInput () throws LineUnavailableException{
// getAudioInfo(); // prints lots of useless information
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,sampleRate,8,1,1,sampleRate,false);
DataLine.Info dlinfo = new DataLine.Info(TargetDataLine.class,
format);
if ( AudioSystem.isLineSupported(dlinfo) ){
targetDataLine = (TargetDataLine)AudioSystem.getLine(dlinfo);
}
targetDataLine.open(format,bufferSize);
bufferSize=targetDataLine.getBufferSize();
gui = new DrumSoundDetectorDemo();
gui.setVirtualDrummerMicrophoneInput(this);
}
示例2: init
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
private static void init() {
try {
// 44,100 samples per second, 16-bit audio, mono, signed PCM, little
// Endian
AudioFormat format = new AudioFormat((float) SAMPLE_RATE, BITS_PER_SAMPLE, 1, true, false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format, SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE);
// the internal buffer is a fraction of the actual buffer size, this
// choice is arbitrary
// it gets divided because we can't expect the buffered data to line
// up exactly with when
// the sound card decides to push out its samples.
buffer = new byte[SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE / 3];
} catch (LineUnavailableException e) {
System.out.println(e.getMessage());
}
// no sound gets made before this call
line.start();
}
示例3: playRecorded
import javax.sound.sampled.AudioSystem; //导入方法依赖的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();
}
示例4: createSourceDataLine
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
private boolean createSourceDataLine() {
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()");
try {
DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat);
if (!(AudioSystem.isLineSupported(info)) ) {
if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat);
// fail silently
return false;
}
SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
} catch (Exception e) {
if (DEBUG || Printer.err)e.printStackTrace();
// fail silently
return false;
}
if (datapusher==null) {
// fail silently
return false;
}
if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine.");
return true;
}
示例5: ExtendedClip
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
public ExtendedClip(JuggleMasterPro objPjuggleMasterPro, byte bytPsoundFileIndex) {
this.bytGsoundFileIndex = bytPsoundFileIndex;
try {
final AudioInputStream objLaudioInputStream =
AudioSystem.getAudioInputStream(new File(Strings.doConcat( objPjuggleMasterPro.strS_CODE_BASE,
Constants.strS_FILE_NAME_A[Constants.intS_FILE_FOLDER_SOUNDS],
objPjuggleMasterPro.chrGpathSeparator,
Constants.strS_FILE_SOUND_NAME_A[bytPsoundFileIndex])));
final AudioFormat objLaudioFormat = objLaudioInputStream.getFormat();
final DataLine.Info objLdataLineInfo =
new DataLine.Info(Clip.class, objLaudioFormat, (int) objLaudioInputStream.getFrameLength()
* objLaudioFormat.getFrameSize());
this.objGclip = (Clip) AudioSystem.getLine(objLdataLineInfo);
this.objGclip.open(objLaudioInputStream);
} catch (final Throwable objPthrowable) {
Tools.err("Error while initializing sound : ", Constants.strS_FILE_SOUND_NAME_A[bytPsoundFileIndex]);
this.objGclip = null;
}
}
示例6: test
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
private static void test(final AudioFormat format, final byte[] data)
throws Exception {
final Line.Info info = new DataLine.Info(Clip.class, format);
final Clip clip = (Clip) AudioSystem.getLine(info);
go = new CountDownLatch(1);
clip.addLineListener(event -> {
if (event.getType().equals(LineEvent.Type.START)) {
go.countDown();
}
});
clip.open(format, data, 0, data.length);
clip.start();
go.await();
while (clip.isRunning()) {
// This loop should not hang
}
while (clip.isActive()) {
// This loop should not hang
}
clip.close();
}
示例7: loadDataLine
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
private void loadDataLine() {
final DataLine.Info dataInfo = new DataLine.Info(SourceDataLine.class, this.sound.getFormat());
try {
this.dataLine = (SourceDataLine) AudioSystem.getLine(dataInfo);
this.dataLine.open();
} catch (final LineUnavailableException e) {
log.log(Level.SEVERE, e.getMessage(), e);
}
}
示例8: playSound
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
public static void playSound(String filename) {
URL resource = ClassLoader.getSystemClassLoader().getResource(filename);
try {
final Clip clip = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
clip.addLineListener(event -> {
if (event.getType() == LineEvent.Type.STOP) {
clip.close();
}
});
clip.open(AudioSystem.getAudioInputStream(resource));
clip.start();
} catch (Exception e) {
logger.error("Failed to play sound " + filename, e);
}
}
示例9: record
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
private byte[] record() throws LineUnavailableException {
AudioFormat format = AudioUtil.getAudioFormat(audioConf);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
// Checks if system supports the data line
if (!AudioSystem.isLineSupported(info)) {
LOGGER.error("Line not supported");
System.exit(0);
}
microphone = (TargetDataLine) AudioSystem.getLine(info);
microphone.open(format);
microphone.start();
LOGGER.info("Listening, tap enter to stop ...");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int numBytesRead;
byte[] data = new byte[microphone.getBufferSize() / 5];
// Begin audio capture.
microphone.start();
// Here, stopped is a global boolean set by another thread.
while (!stopped) {
// Read the next chunk of data from the TargetDataLine.
numBytesRead = microphone.read(data, 0, data.length);
// Save this chunk of data.
byteArrayOutputStream.write(data, 0, numBytesRead);
}
return byteArrayOutputStream.toByteArray();
}
示例10: initializeMixer
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
/** Initialize local mixer: microphone */
public void initializeMixer() {
audioFormat = new AudioFormat(References.SAMPLE_RATE, References.SAMPLE_SIZE_IN_BITS, References.CHANNELS,
References.SIGNED, References.BIG_ENDIAN);
dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
try {
targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
} catch (Exception e) {
e.printStackTrace();
}
}
示例11: run
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
@Override
public void run() {
try {
AudioFormat af = SoundPacket.defaultFormat;
DataLine.Info info = new DataLine.Info(TargetDataLine.class, null);
mic = (TargetDataLine) (AudioSystem.getLine(info));
mic.open(af);
mic.start();
} catch (Exception e) {
System.out.println("Microfone não detectado.");
JOptionPane.showMessageDialog(rootPane, "Microfone não detectado.", "AVISO: ", JOptionPane.INFORMATION_MESSAGE);
}
for (;;) {
Utils.sleep(10);
if (mic.available() > 0) {
byte[] buff = new byte[SoundPacket.defaultDataLenght];
mic.read(buff, 0, buff.length);
long tot = 0;
for (int i = 0; i < buff.length; i++) {
tot += MicThread.amplification * Math.abs(buff[i]);
}
tot *= 2.5;
tot /= buff.length;
micLev.setValue((int) tot);
}
}
}
示例12: createClip
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
private boolean createClip() {
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createClip()");
try {
DataLine.Info info = new DataLine.Info(Clip.class, loadedAudioFormat);
if (!(AudioSystem.isLineSupported(info)) ) {
if (DEBUG || Printer.err)Printer.err("Clip not supported: "+loadedAudioFormat);
// fail silently
return false;
}
Object line = AudioSystem.getLine(info);
if (!(line instanceof AutoClosingClip)) {
if (DEBUG || Printer.err)Printer.err("Clip is not auto closing!"+clip);
// fail -> will try with SourceDataLine
return false;
}
clip = (AutoClosingClip) line;
clip.setAutoClosing(true);
if (DEBUG || Printer.debug) clip.addLineListener(this);
} catch (Exception e) {
if (DEBUG || Printer.err)e.printStackTrace();
// fail silently
return false;
}
if (clip==null) {
// fail silently
return false;
}
if (DEBUG || Printer.debug)Printer.debug("Loaded clip.");
return true;
}
示例13: main
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
boolean realTest = false;
if (!isSoundcardInstalled()) {
return;
}
try {
out("4661602: Buffersize is checked when re-opening line");
AudioFormat format = new AudioFormat(44100, 16, 2, true, false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
SourceDataLine sdl = (SourceDataLine) AudioSystem.getLine(info);
out("Opening with buffersize 12000...");
sdl.open(format, 12000);
out("Opening with buffersize 11000...");
realTest=true;
sdl.open(format, 11000);
try {
sdl.close();
} catch(Throwable t) {}
} catch (Exception e) {
e.printStackTrace();
// do not fail if no audio device installed - bug 4742021
if (realTest || !(e instanceof LineUnavailableException)) {
throw e;
}
}
out("Test passed");
}
示例14: testMixer
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
private static boolean testMixer(Mixer mixer, Class lineType,
String providerClassName,
String instanceName) {
boolean allOk = true;
try {
String propertyValue = (providerClassName != null) ? providerClassName: "" ;
propertyValue += "#" + instanceName;
out("property value: " + propertyValue);
System.setProperty(lineType.getName(), propertyValue);
Line line = null;
Line.Info info = null;
Line.Info[] infos;
AudioFormat format = null;
if (lineType == SourceDataLine.class || lineType == Clip.class) {
infos = mixer.getSourceLineInfo();
format = getFirstLinearFormat(infos);
info = new DataLine.Info(lineType, format);
} else if (lineType == TargetDataLine.class) {
infos = mixer.getTargetLineInfo();
format = getFirstLinearFormat(infos);
info = new DataLine.Info(lineType, format);
} else if (lineType == Port.class) {
/* Actually, a Ports Mixer commonly has source infos
as well as target infos. We ignore this here, since we
just need a random one. */
infos = mixer.getSourceLineInfo();
for (int i = 0; i < infos.length; i++) {
if (infos[i] instanceof Port.Info) {
info = infos[i];
break;
}
}
}
out("Line.Info: " + info);
line = AudioSystem.getLine(info);
out("line: " + line);
if (! lineType.isInstance(line)) {
out("type " + lineType + " failed: class should be '" +
lineType + "' but is '" + line.getClass() + "'!");
allOk = false;
}
} catch (Exception e) {
out("Exception thrown; Test NOT failed.");
e.printStackTrace();
}
return allOk;
}
示例15: main
import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
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.");
}