2014-05-24 2 views
1

그래서, 나는 혀를 보내는 마이크 데이터를 찾고 있었지만 아무 것도 찾지 못했습니다. 그래서 저는 Oracles이 라인 열기에 관해서 tut을 읽었고 오디오를 ByteArrayOutputStream에 녹음 할 수 있었지만 지금은 두 가지 문제가 있습니다!UDP를 통해 오디오를 보내십시오

처음 : 녹음 된 오디오를 재생하는 방법.

두 번째 : 내가 BAOS에 어떻게 녹음하면 동적으로 보내겠습니까? 데이터 배열을 보내 겠지만 프로세서를 수신 할 때마다 BAOS에 쓰는 것이 너무 프로세서가 많을까요? 아니면 다르게 할 수 있습니까?

현재 코드 : 주어진 어떤 도움

import java.io.ByteArrayOutputStream; 

import javax.sound.sampled.AudioFormat; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.DataLine; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.TargetDataLine; 

public class MicrophoneRecorder { 
    static boolean stopped = false; 

    public static void main(String[] args) { 
     AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true); 
     TargetDataLine line = null; 
     DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); 
     if (!AudioSystem.isLineSupported(info)) { 
      System.out.println("Not supported!"); 
     } 
     try { 
      line = (TargetDataLine) AudioSystem.getLine(info); 
      line.open(format); 
     } catch (LineUnavailableException ex) { 
      ex.printStackTrace(); 
     } 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     int numBytesRead = 0; 
     byte[] data = new byte[line.getBufferSize()/5]; 
     line.start(); 

     new Thread(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        Thread.sleep(3000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       stopped = true; 
      } 
     }).start(); 
     while (!stopped) { 
      numBytesRead = line.read(data, 0, data.length); 
      out.write(data, 0, numBytesRead); 
     } 
    } 

} 

감사합니다. 감사합니다. Roberto Anić Banić

P. 이 경우 작동하지 않습니다. http://javasolution.blogspot.com/2007/04/voice-chat-using-java.html P.P.S. UDP는 좋은 선택이며 RTSP를 사용해야합니까?

+0

왜 그 일을하지 않습니다? 당신은 아마 그것을하고 싶다 ... –

+0

코드가 망가져있다 –

+0

오히려 바퀴를 재발 명하는 것 같다 : [RTSP] (http://www.ietf.org/rfc/rfc2326.txt) – marko

답변

2

다음은 UDP를 통해 오디오를 전송하는 방법입니다. 다음은 클라이언트 및 서버 코드입니다. 기본적으로 클라이언트 코드는 캡처 된 오디오를 서버로 보내고 수신시이를 재생합니다. 클라이언트는 캡처 된 오디오를 재생할 수도 있습니다.

클라이언트 코드 : VUClient.java

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
import javax.sound.sampled.*; 

public class VUClient extends JFrame { 

boolean stopaudioCapture = false; 
ByteArrayOutputStream byteOutputStream; 
AudioFormat adFormat; 
TargetDataLine targetDataLine; 
AudioInputStream InputStream; 
SourceDataLine sourceLine; 
Graphics g; 

public static void main(String args[]) { 
    new VUClient(); 
} 

public VUClient() { 
    final JButton capture = new JButton("Capture"); 
    final JButton stop = new JButton("Stop"); 
    final JButton play = new JButton("Playback"); 

    capture.setEnabled(true); 
    stop.setEnabled(false); 
    play.setEnabled(false); 

    capture.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      capture.setEnabled(false); 
      stop.setEnabled(true); 
      play.setEnabled(false); 
      captureAudio(); 
     } 
    }); 
    getContentPane().add(capture); 

    stop.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      capture.setEnabled(true); 
      stop.setEnabled(false); 
      play.setEnabled(true); 
      stopaudioCapture = true; 
      targetDataLine.close(); 
     } 
    }); 
    getContentPane().add(stop); 

    play.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      playAudio(); 
     } 
    }); 
    getContentPane().add(play); 

    getContentPane().setLayout(new FlowLayout()); 
    setTitle("Capture/Playback Demo"); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setSize(400, 100); 
    getContentPane().setBackground(Color.white); 
    setVisible(true); 

    g = (Graphics) this.getGraphics(); 
} 

private void captureAudio() { 
    try { 
     adFormat = getAudioFormat(); 
     DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, adFormat); 
     targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo); 
     targetDataLine.open(adFormat); 
     targetDataLine.start(); 

     Thread captureThread = new Thread(new CaptureThread()); 
     captureThread.start(); 
    } catch (Exception e) { 
     StackTraceElement stackEle[] = e.getStackTrace(); 
     for (StackTraceElement val : stackEle) { 
      System.out.println(val); 
     } 
     System.exit(0); 
    } 
} 

private void playAudio() { 
    try { 
     byte audioData[] = byteOutputStream.toByteArray(); 
     InputStream byteInputStream = new ByteArrayInputStream(audioData); 
     AudioFormat adFormat = getAudioFormat(); 
     InputStream = new AudioInputStream(byteInputStream, adFormat, audioData.length/adFormat.getFrameSize()); 
     DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, adFormat); 
     sourceLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo); 
     sourceLine.open(adFormat); 
     sourceLine.start(); 
     Thread playThread = new Thread(new PlayThread()); 
     playThread.start(); 
    } catch (Exception e) { 
     System.out.println(e); 
     System.exit(0); 
    } 
} 

private AudioFormat getAudioFormat() { 
    float sampleRate = 16000.0F; 
    int sampleInbits = 16; 
    int channels = 1; 
    boolean signed = true; 
    boolean bigEndian = false; 
    return new AudioFormat(sampleRate, sampleInbits, channels, signed, bigEndian); 
} 

class CaptureThread extends Thread { 

    byte tempBuffer[] = new byte[10000]; 

    public void run() { 

     byteOutputStream = new ByteArrayOutputStream(); 
     stopaudioCapture = false; 
     try { 
      DatagramSocket clientSocket = new DatagramSocket(8786); 
      InetAddress IPAddress = InetAddress.getByName("127.0.0.1"); 
      while (!stopaudioCapture) { 
       int cnt = targetDataLine.read(tempBuffer, 0, tempBuffer.length); 
       if (cnt > 0) { 
        DatagramPacket sendPacket = new DatagramPacket(tempBuffer, tempBuffer.length, IPAddress, 9786); 
        clientSocket.send(sendPacket); 
        byteOutputStream.write(tempBuffer, 0, cnt); 
       } 
      } 
      byteOutputStream.close(); 
     } catch (Exception e) { 
      System.out.println("CaptureThread::run()" + e); 
      System.exit(0); 
     } 
    } 
} 

class PlayThread extends Thread { 

    byte tempBuffer[] = new byte[10000]; 

    public void run() { 
     try { 
      int cnt; 
      while ((cnt = InputStream.read(tempBuffer, 0, tempBuffer.length)) != -1) { 
       if (cnt > 0) { 
        sourceLine.write(tempBuffer, 0, cnt); 
       } 
      } 
      //    sourceLine.drain(); 
      //    sourceLine.close(); 
     } catch (Exception e) { 
      System.out.println(e); 
      System.exit(0); 
     } 
    } 
} 
} 

서버 코드 : VUServer.java

import java.io.*; 
import java.net.*; 
import javax.sound.sampled.*; 

public class VUServer { 

ByteArrayOutputStream byteOutputStream; 
AudioFormat adFormat; 
TargetDataLine targetDataLine; 
AudioInputStream InputStream; 
SourceDataLine sourceLine; 

private AudioFormat getAudioFormat() { 
    float sampleRate = 16000.0F; 
    int sampleInbits = 16; 
    int channels = 1; 
    boolean signed = true; 
    boolean bigEndian = false; 
    return new AudioFormat(sampleRate, sampleInbits, channels, signed, bigEndian); 
} 

public static void main(String args[]) { 
    new VUServer().runVOIP(); 
} 

public void runVOIP() { 
    try { 
     DatagramSocket serverSocket = new DatagramSocket(9786); 
     byte[] receiveData = new byte[10000]; 
     while (true) { 
      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 
      serverSocket.receive(receivePacket); 
      System.out.println("RECEIVED: " + receivePacket.getAddress().getHostAddress() + " " + receivePacket.getPort()); 
      try { 
       byte audioData[] = receivePacket.getData(); 
       InputStream byteInputStream = new ByteArrayInputStream(audioData); 
       AudioFormat adFormat = getAudioFormat(); 
       InputStream = new AudioInputStream(byteInputStream, adFormat, audioData.length/adFormat.getFrameSize()); 
       DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, adFormat); 
       sourceLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo); 
       sourceLine.open(adFormat); 
       sourceLine.start(); 
       Thread playThread = new Thread(new PlayThread()); 
       playThread.start(); 
      } catch (Exception e) { 
       System.out.println(e); 
       System.exit(0); 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

class PlayThread extends Thread { 

    byte tempBuffer[] = new byte[10000]; 

    public void run() { 
     try { 
      int cnt; 
      while ((cnt = InputStream.read(tempBuffer, 0, tempBuffer.length)) != -1) { 
       if (cnt > 0) { 
        sourceLine.write(tempBuffer, 0, cnt); 
       } 
      } 
      // sourceLine.drain(); 
      // sourceLine.close(); 
     } catch (Exception e) { 
      System.out.println(e); 
      System.exit(0); 
     } 
    } 
} 
} 
+0

서버가 충돌 함 tempBuffer 한도 때문에 5 초가 지난 후에 어떻게 해결할 수 있습니까? – Asim

+0

출력을 표시 할 수 있습니까? 지금까지 나는 이것에 어떤 문제도 직면하지 않았다. –

관련 문제