使用Java从麦克风流式传输音频

时间:2014-10-24 14:54:33

标签: java sockets audio streaming audio-streaming

我正在开发一个项目,要求我将麦克风的音频从客户端流式传输到服务器。下面的代码就是我写的。当我同时运行客户端和服务器代码时,音频不会实时流式传输。事实上,来自客户端的音频存储在缓冲区中,当我终止客户端代码的执行时,来自服务器缓冲区的音频输出到扬声器。我究竟做错了什么? (我正在开发日食)

服务器

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

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

//import org.apache.commons.io.output.ByteArrayOutputStream;


public class ServerStream {
    private OutgoingSoudnListener osl = new OutgoingSoudnListener();
    boolean outVoice = true;
    AudioFormat format = getAudioFormat();
    private ServerSocket serverSocket;
    Socket server;


    private AudioFormat getAudioFormat() {
        float sampleRate = 16000.0F;
        int sampleSizeBits = 16;
        int channels = 1;
        boolean signed = true;
        boolean bigEndian = false;

        return new AudioFormat(sampleRate, sampleSizeBits, channels, signed, bigEndian);
    }
    public ServerStream() throws IOException{
        try{
            System.out.println("Creating Socket...");
            serverSocket = new ServerSocket(3000);
            osl.runSender();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    class OutgoingSoudnListener{
        public void runSender(){
            try{
                server = serverSocket.accept();
                System.out.println("Listening from mic.");
                DataOutputStream out = new DataOutputStream(server.getOutputStream());
                DataLine.Info micInfo = new DataLine.Info(TargetDataLine.class,format);
                TargetDataLine mic = (TargetDataLine) AudioSystem.getLine(micInfo);
                mic.open(format);
                System.out.println("Mic open.");
                byte tmpBuff[] = new byte[mic.getBufferSize()/5];
                mic.start();
                while(outVoice) {
                    System.out.println("Reading from mic.");
                    int count = mic.read(tmpBuff,0,tmpBuff.length);
                    if (count > 0){
                        System.out.println("Writing buffer to server.");
                        out.write(tmpBuff, 0, count);
                    }               
                    }
                mic.drain();
                mic.close();
                System.out.println("Stopped listening from mic.");
            }catch(Exception e){
                e.printStackTrace();
            }
        }

    }
    public static void main (String args[]) throws IOException{
        new ServerStream();

    }


}

客户端:

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;


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 org.apache.commons.io.IOUtils;

public class ClientStream{

    public ClientStream() throws IOException{
        isl.runListener();
    }

    private IncomingSoundListener isl = new IncomingSoundListener();    
    AudioFormat format = getAudioFormat();
    InputStream is;
    Socket client;
    String serverName = "192.168.2.8";
    int port=3000;
    boolean inVoice = true;


    private AudioFormat getAudioFormat(){
        float sampleRate = 16000.0F;
        int sampleSizeBits = 16;
        int channels = 1;
        boolean signed = true;
        boolean bigEndian = false;

        return new AudioFormat(sampleRate, sampleSizeBits, channels, signed, bigEndian);
    }
    class IncomingSoundListener {
        public void runListener(){
            try{
                System.out.println("Connecting to server:"+serverName+" Port:"+port);
                client = new Socket(serverName,port); 
                System.out.println("Connected to: "+client.getRemoteSocketAddress());
                System.out.println("Listening for incoming audio.");
                DataLine.Info speakerInfo = new DataLine.Info(SourceDataLine.class,format);
                SourceDataLine speaker = (SourceDataLine) AudioSystem.getLine(speakerInfo);
                speaker.open(format);
                speaker.start();
                while(inVoice){
                    is = client.getInputStream();
                    byte[] data = IOUtils.toByteArray(is);  
                    ByteArrayInputStream bais = new ByteArrayInputStream(data);
                    AudioInputStream ais = new AudioInputStream(bais,format,data.length);
                    int bytesRead = 0;
                    if((bytesRead = ais.read(data)) != -1){
                        System.out.println("Writing to audio output.");
                        speaker.write(data,0,bytesRead);

       //                 bais.reset();
                    }
                    ais.close();
                    bais.close();

                }
               speaker.drain();
               speaker.close();
               System.out.println("Stopped listening to incoming audio.");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    public static void main(String [] args) throws IOException{
            new ClientStream();
        }
    }

2 个答案:

答案 0 :(得分:1)

问题出在客户端, 在行

byte[] data = IOUtils.toByteArray(is);

它处理对象本身,而不处理内容。 所以,你必须改变它:

byte[] data = new byte[1024];

答案 1 :(得分:0)

我对此不熟悉,所以如果我离开这里不要生气,但是读取DataLine的api它似乎就像一个缓冲区,你必须在这种情况下冲洗或排水才能获得输出。您是否尝试将mic.drain()/ speaker.drain()命令放入while循环?