使用Java检测来自麦克风的声音。Java版本的问题
我正在运行这个简单的代码,它监听我的麦克风并输出字节 问题: 它会正确地监听麦克风上的任何更改 java版本“1.6.0_43” 但它不会正确地收听麦克风上的任何更改 java版本“1.7.0_25” 代码可以在两个版本上编译并运行良好 使用Java1.6的示例输出使用Java检测来自麦克风的声音。Java版本的问题,java,audio,javasound,Java,Audio,Javasound,我正在运行这个简单的代码,它监听我的麦克风并输出字节 问题: 它会正确地监听麦克风上的任何更改 java版本“1.6.0_43” 但它不会正确地收听麦克风上的任何更改 java版本“1.7.0_25” 代码可以在两个版本上编译并运行良好 使用Java1.6的示例输出 5172 1 5196 2 5103 3 28594 25495 2742 6 31127 5028 4515 9 3856 10 5189 11 5080 12 5165 13 4925 14 当它下降到2500左右时,声音进
- 5172 1
- 5196 2
- 5103 3
- 28594
- 25495
- 2742 6
- 31127
- 5028
- 4515 9
- 3856 10
- 5189 11
- 5080 12
- 5165 13
- 4925 14
import java.io.*;
import javax.sound.sampled.*;
public class SpeechDetectionTest {
public static void main(String[] args) {
ByteArrayOutputStream byteArrayOutputStream;
TargetDataLine targetDataLine;
int cnt;
boolean stopCapture = false;
byte tempBuffer[] = new byte[8000];
int countzero, countdownTimer;
short convert[] = new short[tempBuffer.length];
try {
byteArrayOutputStream = new ByteArrayOutputStream();
stopCapture = false;
countdownTimer = 0;
while (!stopCapture) {
AudioFormat audioFormat = new AudioFormat(8000.0F, 16, 1, true, false);
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
cnt = targetDataLine.read(tempBuffer, 0, tempBuffer.length);
byteArrayOutputStream.write(tempBuffer, 0, cnt);
try {
countzero = 0;
for (int i = 0; i < tempBuffer.length; i++) {
convert[i] = tempBuffer[i];
if (convert[i] == 0) {
countzero++;
}
}
countdownTimer++;
System.out.println(countzero + " " + countdownTimer);
} catch (StringIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
}
Thread.sleep(0);
targetDataLine.close();
}
} catch (Exception e) {
System.out.println(e);
}
}
}
使用Java1.7的输出示例
- 56521
- 56552
- 56993
- 60814
- 6213 5
- 59726
- 5907 7
- 58288
- 59319
- 618710
- 601511
- 5949 12
- 6196 13
import java.io.*;
import javax.sound.sampled.*;
public class SpeechDetectionTest {
public static void main(String[] args) {
ByteArrayOutputStream byteArrayOutputStream;
TargetDataLine targetDataLine;
int cnt;
boolean stopCapture = false;
byte tempBuffer[] = new byte[8000];
int countzero, countdownTimer;
short convert[] = new short[tempBuffer.length];
try {
byteArrayOutputStream = new ByteArrayOutputStream();
stopCapture = false;
countdownTimer = 0;
while (!stopCapture) {
AudioFormat audioFormat = new AudioFormat(8000.0F, 16, 1, true, false);
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
cnt = targetDataLine.read(tempBuffer, 0, tempBuffer.length);
byteArrayOutputStream.write(tempBuffer, 0, cnt);
try {
countzero = 0;
for (int i = 0; i < tempBuffer.length; i++) {
convert[i] = tempBuffer[i];
if (convert[i] == 0) {
countzero++;
}
}
countdownTimer++;
System.out.println(countzero + " " + countdownTimer);
} catch (StringIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
}
Thread.sleep(0);
targetDataLine.close();
}
} catch (Exception e) {
System.out.println(e);
}
}
}
import java.io.*;
导入javax.sound.sampled.*;
公共类语音检测测试{
公共静态void main(字符串[]args){
ByteArrayOutputStream ByteArrayOutputStream;
TargetDataLine TargetDataLine;
int-cnt;
布尔stopCapture=false;
字节tempBuffer[]=新字节[8000];
int countzero,倒计时;
short convert[]=新的short[tempBuffer.length];
试一试{
byteArrayOutputStream=新建byteArrayOutputStream();
stopCapture=false;
倒计时=0;
而(!stopCapture){
AudioFormat AudioFormat=新的AudioFormat(8000.0F,16,1,真,假);
DataLine.Info dataLineInfo=新的DataLine.Info(TargetDataLine.class,audioFormat);
targetDataLine=(targetDataLine)AudioSystem.getLine(dataLineInfo);
targetDataLine.open(音频格式);
targetDataLine.start();
cnt=targetDataLine.read(tempBuffer,0,tempBuffer.length);
写入(tempBuffer,0,cnt);
试一试{
countzero=0;
对于(inti=0;i
有什么想法吗
也张贴在这些论坛上,但没有运气=(
这不是java版本的问题。您将字节错误地转换为短字节。您使用16位格式打开行,并间接使用读取字节来构建16位示例。