You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Audioaufnahme.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package model;
  7. import java.io.ByteArrayInputStream;
  8. import java.io.ByteArrayOutputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.util.concurrent.ExecutorService;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import javax.sound.sampled.AudioFormat;
  15. import javax.sound.sampled.AudioInputStream;
  16. import javax.sound.sampled.AudioSystem;
  17. import javax.sound.sampled.DataLine;
  18. import javax.sound.sampled.LineUnavailableException;
  19. import javax.sound.sampled.SourceDataLine;
  20. import javax.sound.sampled.TargetDataLine;
  21. /**
  22. *
  23. * @author Jan
  24. */
  25. public class Audioaufnahme implements Runnable
  26. {
  27. AudioFormat format;
  28. TargetDataLine mic;
  29. AudioInputStream audioInput;
  30. //AudioOutputStream audioOutput;
  31. SourceDataLine sourceData;
  32. private ExecutorService audioausgabe;
  33. ByteArrayOutputStream out;
  34. public Audioaufnahme()
  35. {
  36. float sampleRate = 48000;
  37. int sampleSizeInBits = 16;
  38. int channels = 1;
  39. boolean signed = true;
  40. boolean bigEndian = true;
  41. try
  42. {
  43. format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
  44. mic = AudioSystem.getTargetDataLine(format);
  45. DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
  46. mic = (TargetDataLine) AudioSystem.getLine(info);
  47. }
  48. catch (Exception e)
  49. {
  50. System.out.println("Fehler bei dem Audioformat des Mikrophones");
  51. Logger.getLogger(Audioaufnahme.class.getName()).log(Level.SEVERE, null, e);
  52. }
  53. }
  54. public void startaufnahme()
  55. {
  56. out = new ByteArrayOutputStream();
  57. try
  58. {
  59. mic.open(format);
  60. int numBytesRead;
  61. int CHUNCK_SIZE = 1024;
  62. byte[] data = new byte[mic.getBufferSize() / 5];
  63. mic.start();
  64. int bytesRead = 0;
  65. try
  66. {
  67. while (bytesRead < 1000000)
  68. {
  69. numBytesRead = mic.read(data, 0, CHUNCK_SIZE);
  70. bytesRead = bytesRead + numBytesRead;
  71. out.write(data, 0, numBytesRead);
  72. }
  73. }
  74. catch (Exception e)
  75. {
  76. e.printStackTrace();
  77. }
  78. mic.close();
  79. }
  80. catch (LineUnavailableException ex)
  81. {
  82. Logger.getLogger(Audioaufnahme.class.getName()).log(Level.SEVERE, null, ex);
  83. }
  84. }
  85. public void playAudio()
  86. {
  87. try
  88. {
  89. byte audioData[] = out.toByteArray();
  90. InputStream byteArrayInputStream = new ByteArrayInputStream(
  91. audioData);
  92. audioInput = new AudioInputStream(byteArrayInputStream, format, audioData.length / format.getFrameSize());
  93. DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
  94. sourceData = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
  95. sourceData.open(format);
  96. sourceData.start();
  97. int cnt = 0;
  98. byte tempBuffer[] = new byte[1000000];
  99. try
  100. {
  101. while ((cnt = audioInput.read(tempBuffer, 0, tempBuffer.length)) != -1)
  102. {
  103. if (cnt > 0)
  104. {
  105. sourceData.write(tempBuffer, 0, cnt);
  106. }
  107. }
  108. }
  109. catch (IOException e)
  110. {
  111. e.printStackTrace();
  112. }
  113. sourceData.drain();
  114. sourceData.close();
  115. }
  116. catch (Exception e)
  117. {
  118. }
  119. }
  120. @Override
  121. public void run()
  122. {
  123. }
  124. }