Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9171743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:12:17+00:00 2026-06-17T16:12:17+00:00

Im try to pass audio between sockets but it pass one time in client

  • 0

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


 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:12:18+00:00Added an answer on June 17, 2026 at 4:12 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to pass more than one variable to other web pages. I try
I try to pass a NSString from one view to an other view, but
If I try to pass data from one activity to another, but before going
I can pass basic data between classes, but when I try to pass a
I try to pass some value from one page to another but at the
What is the difference between ',' and 'as' in except statements, eg: try: pass
I try to pass variable from jquery to code c# but something is wrong.
I'm try to pass a parameter while navigate between pages.. in the firs page
I'm working on a program where I try to pass parameters by reference. I'm
I try to pass a bundle between two activities. This bundle contains a Serializable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.