简体   繁体   English

如何在Java中的套接字之间传递音频

[英]how to pass audio between sockets in java

Im try to pass audio between sockets but it pass one time in client than clients stops working. 我尝试在套接字之间传递音频,但在客户端传递了一次,即客户端停止工作。 I think the way im writing and sending may be wrong please help me regarding this code Following is my server code : 我认为即时通讯编写和发送方式可能有误,请就此代码提供帮助,以下是我的服务器代码:

  public void run(){
    try{
            SetInitialSettings();
            int bufferSize = (int) format.getSampleRate() * (int) format.getFrameSize();
            System.out.println("length :" +bufferSize);
            out = new ByteArrayOutputStream();
            readagain=true;
            while(record){
            int count=0;

            for(int i=0;i<1000;i++)
            {

                       if(buffer==null){
                       buffer = new byte[bufferSize];
                       }
                       count = line.read(buffer, 0, buffer.length);
                       if (count > 0) {
                       //out.write(buffer, 0, count);
                       //readagain=false;
                           for (int j=0;i<ser.clientReceiver.audioSender.size();j++){
                            ser.clientReceiver.audioSender.get(i).cliOut.write("Audio".getBytes());
                            ser.clientReceiver.audioSender.get(i).cliOut.write('\n');
                            ser.clientReceiver.audioSender.get(i).cliOut.flush();
                            ser.clientReceiver.audioSender.get(i).cliOut.write(buffer,0,count);
                            ser.clientReceiver.audioSender.get(i).cliOut.flush();
                            Thread.sleep(2000);

                               }
                       }

            }            




    }
    }
    catch(Exception ex)
    {
        System.out.println(ex.getMessage());

    }




    }

and following is my client code : 以下是我的客户代码:

public void run(){ public void run(){

    try{
        //audioReceiver = new TAudioReceiver(cli);
        //audioReceiver.start();
        while(true){

            //Thread.sleep(1000);    
            System.out.println("reading token");
            cmd= cli.BufferedReader().readLine();
            System.out.println("token :" +cmd);
            switch (cmd) {
                case "Msg":
                    message = cli.BufferedReader().readLine();
                    cmd="None";
                    break;
            case "File":
              GetFile();
                break;
            case "Audio":
                PlayAudio();
                break;


        }


        }

        //reading msgs here
    }
    catch(Exception ex)
    {
        //throw new InterruptedException(ex.getMessage());
    }
}

this is the function need to run when "audio" msg received from server to client 这是从服务器到客户端收到“音频”消息时需要运行的功能

 public void PlayAudio()throws Exception{

     try{

                    cli.ServerInputStream().read(buffer);


                    audio = buffer;
                    audioIn = new ByteArrayInputStream(audio);
                    AudioFormat f = new AudioFormat(8000, 8, 1,true, true);
                    ain= new AudioInputStream(audioIn,f, audio.length/f.getFrameSize());
                    DataLine.Info info = new DataLine.Info(SourceDataLine.class, f);
                    try (SourceDataLine line = (SourceDataLine)AudioSystem.getLine(info)) {
                        int bufferSize = (int) f.getSampleRate()* f.getFrameSize();
                        buffer= new byte[bufferSize];
                        int count;
                        while ((count = ain.read(buffer, 0, buffer.length)) != -1) {
                        if (count > 0) {
                          line.write(buffer, 0, count);
                        }
                        }
                        line.drain();
                    }

     }
     catch(Exception ex)
     {
         System.out.println("file" + ex.getMessage());
     }


 }

Your original code was better. 您的原始代码更好。 You must write(buffer, 0, count), not write(buffer). 您必须写入(缓冲区,0,计数),而不是写入(缓冲区)。 Otherwise you are writing junk at the end of each buffer that wasn't read by the last read. 否则,您将在最后一次读取未读取的每个缓冲区的末尾写入垃圾。 Your second piece of code ignores the result of read(). 您的第二段代码将忽略read()的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM