Pregunta

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

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


 }
¿Fue útil?

Solución

Your original code was better. You must write(buffer, 0, count), not write(buffer). 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().

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top