1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- /*
- * 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.concurrent.Executors;
- 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.SourceDataLine;
- import javax.sound.sampled.TargetDataLine;
-
- /**
- *
- * @author Jan
- */
- public class Audioausgabe implements Runnable
- {
- AudioFormat format;
- AudioInputStream audioInput;
- SourceDataLine sourceData;
- private ExecutorService audioausgabe;
- ByteArrayOutputStream output;
-
- public Audioausgabe()
- {
- audioausgabe = Executors.newSingleThreadExecutor();
- float sampleRate = 48000;
- int sampleSizeInBits = 16;
- int channels = 1;
- boolean signed = true;
- boolean bigEndian = true;
- try
- {
- format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
- DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
- }
- catch (Exception e)
- {
- System.out.println("Fehler bei dem Audioformat des Mikrophones");
- Logger.getLogger(Audioausgabe.class.getName()).log(Level.SEVERE, null, e);
- }
- }
- public void playAudio()
- {
- try
- {
- byte audioData[] = output.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()
- {
- }
- }
|