本文整理汇总了Java中javax.sound.sampled.TargetDataLine.read方法的典型用法代码示例。如果您正苦于以下问题:Java TargetDataLine.read方法的具体用法?Java TargetDataLine.read怎么用?Java TargetDataLine.read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.sound.sampled.TargetDataLine
的用法示例。
在下文中一共展示了TargetDataLine.read方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: record
import javax.sound.sampled.TargetDataLine; //导入方法依赖的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();
}
示例2: run
import javax.sound.sampled.TargetDataLine; //导入方法依赖的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);
}
}
}
示例3: call
import javax.sound.sampled.TargetDataLine; //导入方法依赖的package包/类
@Override
public void call(final Subscriber<? super byte[]> child) {
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
// checks if system supports the data line
if (!AudioSystem.isLineSupported(info)) {
child.onError(new RuntimeException("line not supported for format "
+ format));
return;
}
try {
TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
System.out.println("Starting capture...");
line.start();
byte[] buffer = new byte[bufferSize];
while (!child.isUnsubscribed() && line.isOpen()) {
int count = line.read(buffer, 0, bufferSize);
if (count > 0)
child.onNext(Arrays.copyOf(buffer, count));
}
child.onCompleted();
line.close();
} catch (LineUnavailableException e) {
child.onError(e);
}
}
示例4: main
import javax.sound.sampled.TargetDataLine; //导入方法依赖的package包/类
public static void main(String[] args) {
// Pedimos los mixers de nuestro sistema
Mixer.Info[] mixersInfo = AudioSystem.getMixerInfo();
for (Mixer.Info info : mixersInfo) {
System.out.println("Mixer: " + info);
}
// Los Line están asociados a los mixers, aca verificamos si el micrófono esta asociado a un mixer
if (AudioSystem.isLineSupported(Port.Info.MICROPHONE)) {
// Creamos el socket de envío en cualquier puerto
try (DatagramSocket socket = new DatagramSocket()) {
// Configuramos el destino del audio
InetAddress address = InetAddress.getLocalHost();
// Creamos un DataLine el cual agrega funcionalidades sobre el Line (Métodos de transporte), el TargetDataLine permite leer audio
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, AudioFormatHelper.getAudioFormat());
// Obtenemos la línea
TargetDataLine targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
// Abrimos la línea
targetDataLine.open(AudioFormatHelper.getAudioFormat());
// El start permite que la línea empiece a usar I/O
targetDataLine.start();
while (isRunning) {
// Creamos un buffer para leer los datos (Usamos 32000 porque el sample lo pusimos en 8000 y 2 bytes por sample = 16000 por segundo)
byte[] buffer = new byte[32000];
// Leemos y enviamos el audio
targetDataLine.read(buffer, 0, buffer.length);
sendAudio(socket, address, buffer);
}
} catch (Exception e) {
// Log and Handle exception
e.printStackTrace();
}
}
}
示例5: run
import javax.sound.sampled.TargetDataLine; //导入方法依赖的package包/类
public void run() {
// Ensure we have a compatible line for our format.
AudioFormat format = Utils.getFormat();
TargetDataLine line = this.getAudioLine(format);
if (null == line) {
System.err.println("Unable to get line");
return;
}
// Record!
ByteArrayOutputStream first_100ms_bytes = new ByteArrayOutputStream();
byte[] data = new byte[Utils.CHUNK_OF_10MS];
int numBytesRead = 0;
int totalBytesRead = 0;
boolean first100Analyzed = false;
line.start();
while (running) {
// System.out.println("listening");
if ((numBytesRead = line.read(data, 0, Utils.CHUNK_OF_10MS)) == -1) {
break;
}
totalBytesRead += numBytesRead;
System.out.println("totalBytesRead: " + totalBytesRead);
baos.write(data, 0, numBytesRead);
// Selectively write the first 100ms to this different buffer
if (first_100ms_bytes.size() < Utils.CHUNK_OF_100_MS) {
first_100ms_bytes.write(data, 0, numBytesRead);
} else {
if (!first100Analyzed) {
processFirst100ms(first_100ms_bytes);
first100Analyzed = true;
}
}
}
// we reached the end of the stream. stop and close the line.
line.stop();
line.close();
line = null;
System.out.println("Line stopped, closed, and nulled");
// stop and close the output stream
try {
baos.flush();
baos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
this.thread = null;
}