使用Java检测麦克风的声音。 Java版本的问题

时间:2013-07-10 23:07:59

标签: java audio javasound

我正在运行这个简单的代码,它可以监听我的麦克风并输出字节。

问题: 它可以正确地监听麦克风上的任何更改 java版“1.6.0_43”

但它没有正确听取麦克风上的任何更改 java版“1.7.0_25”

代码在两个版本上编译并运行良好。

使用Java 1.6的输出示例

  • 5172 1
  • 5196 2
  • 5103 3
  • 2859 4
  • 2549 5
  • 2742 6
  • 3112 7
  • 5028 8
  • 4515 9
  • 3856 10
  • 5189 11
  • 5080 12
  • 5165 13
  • 4925 14

当声音进入麦克风时,它下降到2500左右。

使用Java 1.7的输出示例

  • 5652 1
  • 5655 2
  • 5699 3
  • 6081 4
  • 6213 5
  • 5972 6
  • 5907 7
  • 5828 8
  • 5931 9
  • 6187 10
  • 6015 11
  • 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);
        }
    }
}

有什么想法吗?

也发布在这些论坛上,但没有运气=(

http://www.coderanch.com/t/615528//java/Detecting-sound-microphone-Java-Java#2811088

http://www.dreamincode.net/forums/topic/324610-detecting-sound-from-microphone-using-java-issue-with-java-versions/

0 个答案:

没有答案