从与Java麦克风的音频流 [英] Streaming audio from microphone with Java

查看:1178
本文介绍了从与Java麦克风的音频流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发需要我从客户端流从麦克风音频到服务器的一个项目。下面显示的code是我自己编写。当我运行客户端和服务器code中的声音是不是现场直播。实际上来自客户端的音频存储在缓冲器和当我从服务器上的缓冲器终止客户端code中的音频的执行得到输出到扬声器。我究竟做错了什么? (我在Eclipse上开发)

服务器:

 进口java.io.DataOutputStream中;
进口java.io.IOException异常;
进口java.net.ServerSocket的;
进口的java.net.Socket;进口javax.sound.sampled.AudioFormat中;
进口javax.sound.sampled.AudioSystem;
进口javax.sound.sampled.DataLine中;
进口javax.sound.sampled.TargetDataLine;//进口org.apache.commons.io.output.ByteArrayOutputStream;
公共类ServerStream {
    私人OutgoingSoudnListener OSL =新OutgoingSoudnListener();
    布尔outVoice = TRUE;
    AudioFormat的格式= getAudioFormat();
    私人的ServerSocket ServerSocket的;
    套接字服务器;
    私人AudioFormat的getAudioFormat(){
        浮采样率= 16000.0F;
        INT sampleSizeBits = 16;
        INT频道= 1;
        布尔签订= TRUE;
        布尔大尾端= FALSE;        返回新AudioFormat的(采样率,sampleSizeBits,渠道,签订大尾端);
    }
    公共ServerStream()抛出IOException
        尝试{
            的System.out.println(创建插座......);
            ServerSocket的=新的ServerSocket(3000);
            osl.runSender();
        }赶上(例外五){
            e.printStackTrace();
        }
    }
    类OutgoingSoudnListener {
        公共无效runSender(){
            尝试{
                服务器的ServerSocket.accept =();
                的System.out.println(从话筒听。);
                DataOutputStream类出=新DataOutputStream类(server.getOutputStream());
                DataLine.Info micInfo =新DataLine.Info(TargetDataLine.class,格式);
                TargetDataLine的MIC =(TargetDataLine的)AudioSystem.getLine(micInfo);
                mic.open(格式);
                的System.out.println(话筒打开。);
                字节tmpBuff [] =新的字节[mic.getBufferSize()/ 5];
                mic.start();
                而(outVoice){
                    的System.out.println(从麦克风读);
                    诠释计数= mic.read(tmpBuff,0,tmpBuff.length);
                    如果(计数大于0){
                        的System.out.println(写入缓存服务器。);
                        out.write(tmpBuff,0,计数);
                    }
                    }
                mic.drain();
                mic.close();
                的System.out.println(已停止从话筒听着。);
            }赶上(例外五){
                e.printStackTrace();
            }
        }    }
    公共静态无效的主要(字符串ARGS [])抛出IOException
        新ServerStream();    }
}

客户端:

 进口java.io.ByteArrayInputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口的java.net.Socket;
进口javax.sound.sampled.AudioFormat中;
进口javax.sound.sampled.AudioInputStream中;
进口javax.sound.sampled.AudioSystem;
进口javax.sound.sampled.DataLine中;
进口javax.sound.sampled.SourceDataLine;进口org.apache.commons.io.IOUtils;公共类ClientStream {    公共ClientStream()抛出IOException
        isl.runListener();
    }    私人IncomingSoundListener ISL =新IncomingSoundListener();
    AudioFormat的格式= getAudioFormat();
    InputStream为;
    客户端的Socket;
    字符串SERVERNAME =192.168.2.8;
    INT端口= 3000;
    布尔发票= TRUE;
    私人AudioFormat的getAudioFormat(){
        浮采样率= 16000.0F;
        INT sampleSizeBits = 16;
        INT频道= 1;
        布尔签订= TRUE;
        布尔大尾端= FALSE;        返回新AudioFormat的(采样率,sampleSizeBits,渠道,签订大尾端);
    }
    类IncomingSoundListener {
        公共无效runListener(){
            尝试{
                的System.out.println(连接到服务器服务器名+ +端口:+端口);
                客户端=新的Socket(服务器,端口);
                的System.out.println(连接到:+ client.getRemoteSocketAddress());
                的System.out.println(监听传入的声音。);
                DataLine.Info speakerInfo =新DataLine.Info(SourceDataLine.class,格式);
                SourceDataLine的音箱=(SourceDataLine的)AudioSystem.getLine(speakerInfo);
                speaker.open(格式);
                speaker.start();
                而(发票){
                    是= client.getInputStream();
                    字节[]数据= IOUtils.toByteArray(是);
                    ByteArrayInputStream的BAIS =新ByteArrayInputStream的(数据);
                    AIS的AudioInputStream =新的AudioInputStream(拜斯,格式,data.length);
                    INT读取动作= 0;
                    如果((读取动作= ais.read(数据))!= - 1){
                        的System.out.println(写入音频输出。);
                        speaker.write(数据,0,读取动作);       // bais.reset();
                    }
                    ais.close();
                    bais.close();                }
               speaker.drain();
               speaker.close();
               的System.out.println(停止收听传入的音频。);
            }赶上(例外五){
                e.printStackTrace();
            }
        }
    }
    公共静态无效的主要(字串[] args)抛出IOException
            新ClientStream();
        }
    }


解决方案

问题是在客户端,
在该行

字节[]数据= IOUtils.toByteArray(是);`

它涉及对象本身,而不是与内容。
所以,你必须将其更改为
字节[]数据=新的字节[1024];

I'm developing a project which requires me to stream audio from microphone from a client to a server. The code shown below is what I have written. When I run both the client and server code the audio is not streamed live. In fact the audio from the client is stored in the buffer and when I terminate the execution of the client side code the audio from the buffer on the server gets output to the speaker. What am I doing wrong? (I'm developing on eclipse)

server:

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();

    }


}

client:

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();
        }
    }

解决方案

The problem is in client side, in the line

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

It deals with the object itself, not with the content. So, you must change it to byte[] data = new byte[1024];

这篇关于从与Java麦克风的音频流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆