/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package model; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.concurrent.ExecutorService; import java.util.logging.Level; import java.util.logging.Logger; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.SourceDataLine; import javax.sound.sampled.TargetDataLine; /** * * @author Jan */ public class Audioaufnahme implements Runnable { AudioFormat format; TargetDataLine mic; AudioInputStream audioInput; //AudioOutputStream audioOutput; SourceDataLine sourceData; private ExecutorService audioausgabe; ByteArrayOutputStream out; public Audioaufnahme() { float sampleRate = 48000; int sampleSizeInBits = 16; int channels = 1; boolean signed = true; boolean bigEndian = true; try { format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); mic = AudioSystem.getTargetDataLine(format); DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); mic = (TargetDataLine) AudioSystem.getLine(info); } catch (Exception e) { System.out.println("Fehler bei dem Audioformat des Mikrophones"); Logger.getLogger(Audioaufnahme.class.getName()).log(Level.SEVERE, null, e); } } public void startaufnahme() { out = new ByteArrayOutputStream(); try { mic.open(format); int numBytesRead; int CHUNCK_SIZE = 1024; byte[] data = new byte[mic.getBufferSize() / 5]; mic.start(); int bytesRead = 0; try { while (bytesRead < 1000000) { numBytesRead = mic.read(data, 0, CHUNCK_SIZE); bytesRead = bytesRead + numBytesRead; out.write(data, 0, numBytesRead); } } catch (Exception e) { e.printStackTrace(); } mic.close(); } catch (LineUnavailableException ex) { Logger.getLogger(Audioaufnahme.class.getName()).log(Level.SEVERE, null, ex); } } public void playAudio() { try { byte audioData[] = out.toByteArray(); InputStream byteArrayInputStream = new ByteArrayInputStream( audioData); audioInput = new AudioInputStream(byteArrayInputStream, format, audioData.length / format.getFrameSize()); DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format); sourceData = (SourceDataLine) AudioSystem.getLine(dataLineInfo); sourceData.open(format); sourceData.start(); int cnt = 0; byte tempBuffer[] = new byte[1000000]; try { while ((cnt = audioInput.read(tempBuffer, 0, tempBuffer.length)) != -1) { if (cnt > 0) { sourceData.write(tempBuffer, 0, cnt); } } } catch (IOException e) { e.printStackTrace(); } sourceData.drain(); sourceData.close(); } catch (Exception e) { } } @Override public void run() { } }