Java 实时麦克风音频播放
好吧,我一直在找,但还没有找到解决办法。我已经成功地通过音频格式(44100.0f,32,2,true,true)从话筒获得了一个Java 实时麦克风音频播放,java,audio,Java,Audio,好吧,我一直在找,但还没有找到解决办法。我已经成功地通过音频格式(44100.0f,32,2,true,true)从话筒获得了一个字节[]输出格式。好的,但是现在我想通过我的耳机播放它来测试它 我在StackOverflow上读过很多东西,在Google上搜索过,也在这里查看过,但我找不到一种方法,可以让我已经存储的数据在耳机(或实际的音频输出设备)中播放 我读到过,常用的方法是从剪辑播放(剪辑读取音频文件的文件流),或者使用SourceDataLine。但是我想把字节[]放回输出流或缓冲区,并
字节[]
输出代码>格式。好的,但是现在我想通过我的耳机播放它来测试它
我在StackOverflow上读过很多东西,在Google上搜索过,也在这里查看过,但我找不到一种方法,可以让我已经存储的数据在耳机(或实际的音频输出设备)中播放
我读到过,常用的方法是从剪辑播放(剪辑读取音频文件的文件流),或者使用SourceDataLine。但是我想把字节[]
放回输出流或缓冲区,并实时播放
我的代码打算每秒被调用60次左右(因为它嵌套在3D应用程序中工作,也许我以后会对它进行线程处理),并输出字节[]
,以进行图形表示
我的实际(非工作)方法是(在下面发布完整代码以完成):
此处的完整代码(如果有人认为相关):
package main;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
public class Monitor implements Runnable {
AudioFormat format;
TargetDataLine line;
DataLine.Info info;
byte soundArray[];
public Monitor() {
format = new AudioFormat(44100.0f, 32, 2, true, true);
// format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
// 44100.0F, 16, 2, 4, 44100.0F, false);
try {
line = AudioSystem.getTargetDataLine(format);
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DataLine.Info info = new DataLine.Info(TargetDataLine.class,
format); // format is an AudioFormat object
if (!AudioSystem.isLineSupported(info)) {
// Handle the error ...
}
// Obtain and open the line.
try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
} catch (LineUnavailableException ex) {
// Handle the error ...
}
soundArray = new byte[8000];
}
boolean started;
@Override
public void run() {
try {
line.open();
line.start();
line.read(soundArray, 0, 8000);
System.out.println('A');
printFloatArray(fragment(16, soundArray));
line.stop();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
playOnRealtime();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static byte[] fragment(int n, byte[] array) {
int length = array.length;
byte[] result = new byte[n];
for (int k = 0; k < n; k++) {
result[k] = array[k * (length / n)];
}
return result;
}
private static void printFloatArray(byte[] array) {
for (byte var : array) {
System.out.print(String.format("%3d", var) + "\t");
}
}
public byte[] getData(int n_div) {
return fragment(n_div, soundArray);
}
private void playOnRealtime() throws LineUnavailableException {
Clip clip = AudioSystem.getClip();
TargetDataLine d_line;
AudioInputStream inputStream;
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
format); // format is an AudioFormat object
if (!AudioSystem.isLineSupported(info)) {
System.err.print("Dataline OUT not supported!");
}
try {
d_line = AudioSystem.getTargetDataLine(format);
d_line.open();
d_line.start();
inputStream = AudioSystem.getAudioInputStream(format, new AudioInputStream(d_line));
clip.open(inputStream);
clip.start();
d_line.stop();
clip.stop();
} catch (LineUnavailableException ex) {
// Handle the error.
// ...
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
packagemain;
导入java.io.IOException;
导入javax.sound.sampled.AudioFormat;
导入javax.sound.sampled.AudioInputStream;
导入javax.sound.sampled.AudioSystem;
导入javax.sound.sampled.Clip;
导入javax.sound.sampled.DataLine;
导入javax.sound.sampled.LineUnavailableException;
导入javax.sound.sampled.SourceDataLine;
导入javax.sound.sampled.TargetDataLine;
公共类监视器实现可运行{
音频格式;
目标数据线;
DataLine.Info;
字节数组[];
公共监督员(){
格式=新的音频格式(44100.0f,32,2,真,真);
//格式=新的音频格式(AudioFormat.Encoding.PCM_签名,
//44100.0F、16、2、4、44100.0F、假);
试一试{
line=AudioSystem.getTargetDataLine(格式);
}捕获(LineUnavailableException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
DataLine.Info=newdataline.Info(TargetDataLine.class,
format);//format是AudioFormat对象
如果(!AudioSystem.isLineSupported(信息)){
//处理错误。。。
}
//获取并打开线路。
试一试{
line=(TargetDataLine)AudioSystem.getLine(info);
行。打开(格式);
}捕获(LineUnavailableException ex){
//处理错误。。。
}
soundArray=新字节[8000];
}
布尔启动;
@凌驾
公开募捐{
试一试{
line.open();
line.start();
读取(声音阵列,0,8000);
System.out.println('A');
printFloatArray(片段(16,soundArray));
line.stop();
}捕获(LineUnavailableException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
playOnRealtime();
}捕获(LineUnavailableException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
私有静态字节[]片段(int n,字节[]数组){
int length=array.length;
字节[]结果=新字节[n];
对于(int k=0;k
package main;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
public class Monitor implements Runnable {
AudioFormat format;
TargetDataLine line;
DataLine.Info info;
byte soundArray[];
public Monitor() {
format = new AudioFormat(44100.0f, 32, 2, true, true);
// format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
// 44100.0F, 16, 2, 4, 44100.0F, false);
try {
line = AudioSystem.getTargetDataLine(format);
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DataLine.Info info = new DataLine.Info(TargetDataLine.class,
format); // format is an AudioFormat object
if (!AudioSystem.isLineSupported(info)) {
// Handle the error ...
}
// Obtain and open the line.
try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
} catch (LineUnavailableException ex) {
// Handle the error ...
}
soundArray = new byte[8000];
}
boolean started;
@Override
public void run() {
try {
line.open();
line.start();
line.read(soundArray, 0, 8000);
System.out.println('A');
printFloatArray(fragment(16, soundArray));
line.stop();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
playOnRealtime();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static byte[] fragment(int n, byte[] array) {
int length = array.length;
byte[] result = new byte[n];
for (int k = 0; k < n; k++) {
result[k] = array[k * (length / n)];
}
return result;
}
private static void printFloatArray(byte[] array) {
for (byte var : array) {
System.out.print(String.format("%3d", var) + "\t");
}
}
public byte[] getData(int n_div) {
return fragment(n_div, soundArray);
}
private void playOnRealtime() throws LineUnavailableException {
Clip clip = AudioSystem.getClip();
TargetDataLine d_line;
AudioInputStream inputStream;
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
format); // format is an AudioFormat object
if (!AudioSystem.isLineSupported(info)) {
System.err.print("Dataline OUT not supported!");
}
try {
d_line = AudioSystem.getTargetDataLine(format);
d_line.open();
d_line.start();
inputStream = AudioSystem.getAudioInputStream(format, new AudioInputStream(d_line));
clip.open(inputStream);
clip.start();
d_line.stop();
clip.stop();
} catch (LineUnavailableException ex) {
// Handle the error.
// ...
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}