java - TargetDataline 返回 -1(Audiosystem.NOT_SPECIFIED)
问题描述
我正在尝试制作一个实时语音识别程序,但是当我尝试从 targetdataline 获取输入音量时,它返回 -1。这很奇怪,因为我确实指定了音频系统。
这甚至发生在我直接从文档中复制代码时。这是我的代码:
public void startRecognizer() throws LineUnavailableException {
AudioFormat format = new AudioFormat(16000.0F, 16, 1, true, true);
TargetDataLine line = null;
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); // format is an AudioFormat object
if (!AudioSystem.isLineSupported(info)) {
// Handle the error ...
for (Line.Info i :AudioSystem.getTargetLineInfo(info)) {
System.out.println(i.toString());
}
System.out.println("line not supported, exiting");
System.exit(1);
}
try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format, 4096);
} catch (LineUnavailableException ex) {
System.out.println("line is unavailable, exiting...");
//error code one for line not available.
System.exit(1);
}
System.out.println(line.getLevel());
ByteArrayOutputStream out = new ByteArrayOutputStream();
int numBytesRead= 0;
byte[] data = new byte[line.getBufferSize() / 5];
System.out.println("getting minimum soundlevel...");
float minimumSound = Main.getApi().getConfigManager().getIO().getConfig().getFloat("minimumSound", 0.3F);
while(true){
line.start();
System.out.println(line.getLevel() + ", " + minimumSound);
if (line.getLevel() >= minimumSound){
isWaiting = true;
TargetDataLine finalLine = line;
Thread t = new Thread(() -> {
try {
Thread.sleep(2000);
if (finalLine.getLevel() <= minimumSound) isWaiting = false;
System.out.println("stopped talking..");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
t.start();
while(isWaiting){
numBytesRead = line.read(data, 0, data.length);
out.write(data, 0, numBytesRead);
}
t.stop();
line.stop();
System.out.println(data + ", " + numBytesRead);
}
}
}
并且根本没有错误。
解决方案
推荐阅读
- mongodb - 如何在猫鼬中检查日期是否小于今天的日期?
- eclipse - Log4J2 - 从 Eclipse 运行时不发生日志记录
- kubernetes - 如何将 n 个不同的配置分配给 n 个 pod
- javascript - 使用 msnodesqlv8 nodejs 连接到数据库
- java - 服务器端请求伪造漏洞
- sql - 触发器不会使用来自另一个实例\DB 的数据插入
- java - 为什么这个 JAVA 代码(DoublyLinkedList)是错误的,stackOverflow
- c - 我可以用另一个#define 指令重新定义宏吗?
- json - XSLT 逗号分隔列表,不分组
- python-3.x - matplotlib 笔记本光标坐标图上的双 y 轴