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.

Audioausgabe.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.concurrent.Executors;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15. import javax.sound.sampled.AudioFormat;
  16. import javax.sound.sampled.AudioInputStream;
  17. import javax.sound.sampled.AudioSystem;
  18. import javax.sound.sampled.DataLine;
  19. import javax.sound.sampled.SourceDataLine;
  20. import javax.sound.sampled.TargetDataLine;
  21. /**
  22. *
  23. * @author Jan
  24. */
  25. public class Audioausgabe implements Runnable
  26. {
  27. AudioFormat format;
  28. AudioInputStream audioInput;
  29. SourceDataLine sourceData;
  30. private ExecutorService audioausgabe;
  31. ByteArrayOutputStream output;
  32. public Audioausgabe()
  33. {
  34. audioausgabe = Executors.newSingleThreadExecutor();
  35. float sampleRate = 48000;
  36. int sampleSizeInBits = 16;
  37. int channels = 1;
  38. boolean signed = true;
  39. boolean bigEndian = true;
  40. try
  41. {
  42. format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
  43. DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
  44. }
  45. catch (Exception e)
  46. {
  47. System.out.println("Fehler bei dem Audioformat des Mikrophones");
  48. Logger.getLogger(Audioausgabe.class.getName()).log(Level.SEVERE, null, e);
  49. }
  50. }
  51. public void playAudio()
  52. {
  53. try
  54. {
  55. byte audioData[] = output.toByteArray();
  56. InputStream byteArrayInputStream = new ByteArrayInputStream(
  57. audioData);
  58. audioInput = new AudioInputStream(byteArrayInputStream, format, audioData.length / format.getFrameSize());
  59. DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
  60. sourceData = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
  61. sourceData.open(format);
  62. sourceData.start();
  63. int cnt = 0;
  64. byte tempBuffer[] = new byte[1000000];
  65. try
  66. {
  67. while ((cnt = audioInput.read(tempBuffer, 0, tempBuffer.length)) != -1)
  68. {
  69. if (cnt > 0)
  70. {
  71. sourceData.write(tempBuffer, 0, cnt);
  72. }
  73. }
  74. }
  75. catch (IOException e)
  76. {
  77. e.printStackTrace();
  78. }
  79. sourceData.drain();
  80. sourceData.close();
  81. }
  82. catch (Exception e)
  83. {
  84. }
  85. }
  86. @Override
  87. public void run()
  88. {
  89. }
  90. }