title: Detecting sound from microphone using Java. Issue with Java Versions
body: <p>I'm running this simple code that listens to my microphone and prints out bytes.</p>
  
  <p>Problem:
  It listens to the microphone for any changes correctly on
  java version "1.6.0_43"</p>
  
  <p>but it doesn't listen to the microphone for any changes correctly on
  java version "1.7.0_25"</p>
  
  <p>Code compiles and runs fine on both versions.</p>
  
  <p>Example Output using Java 1.6</p>
  
  <ul>
  <li>5172 1</li>
  <li>5196 2</li>
  <li>5103 3</li>
  <li>2859 4</li>
  <li>2549 5</li>
  <li>2742 6</li>
  <li>3112 7</li>
  <li>5028 8</li>
  <li>4515 9</li>
  <li>3856 10</li>
  <li>5189 11</li>
  <li>5080 12</li>
  <li>5165 13</li>
  <li>4925 14</li>
  </ul>
  
  <p>When it drops down to around 2500 that's when there was sound going into the microphone.</p>
  
  <p>Example Output using Java 1.7</p>
  
  <ul>
  <li>5652 1 </li>
  <li>5655 2 </li>
  <li>5699 3 </li>
  <li>6081 4 </li>
  <li>6213 5 </li>
  <li>5972 6 </li>
  <li>5907 7 </li>
  <li>5828 8 </li>
  <li>5931 9 </li>
  <li>6187 10 </li>
  <li>6015 11 </li>
  <li>5949 12 </li>
  <li>6196 13</li>
  </ul>
  
  <p>It no longer detects any noise from the microphone.</p>
  
  <pre><code>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 &lt; 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);
          }
      }
  }
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>Also posted on these forums, but no luck =(</p>
  
  <p><a href="http://www.coderanch.com/t/615528//java/Detecting-sound-microphone-Java-Java#2811088" rel="nofollow">http://www.coderanch.com/t/615528//java/Detecting-sound-microphone-Java-Java#2811088</a></p>
  
  <p><a href="http://www.dreamincode.net/forums/topic/324610-detecting-sound-from-microphone-using-java-issue-with-java-versions/" rel="nofollow">http://www.dreamincode.net/forums/topic/324610-detecting-sound-from-microphone-using-java-issue-with-java-versions/</a></p>