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

  • Home
  • SEARCH
  • 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 7717601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:56:34+00:00 2026-06-01T02:56:34+00:00

Hi I’ve been writing a chat client and wanted to test the Java Sound

  • 0

Hi I’ve been writing a chat client and wanted to test the Java Sound API. I’ve managed to get sound working from the mic to the speakers on different computers via UDP. However the sound isn’t very clear. To check whether this was because of lost packets etc in the UDP protocol I wrote a small test for the sound to go to the speakers on the same machine as the mic. The sound isn’t any different which makes me think I have some settings wrong for reading or writing the sound. Can anybody have a look at my code and tell me how to make the sound clearer?

package test;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class VoiceTest extends JFrame {
    private JButton chat = new JButton("Voice");
    private GUIListener gl = new GUIListener();
    private IncomingSoundListener isl = new IncomingSoundListener();
    private OutgoingSoundListener osl = new OutgoingSoundListener();
    private boolean inVoice = true;
    private boolean outVoice = false;
    AudioFormat format = getAudioFormat();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

public VoiceTest() throws IOException {
    super ("Test");
    //new Thread(tl).start();
    new Thread(isl).start();
    Container contentPane = this.getContentPane();
    this.setSize(200,100);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    chat.setBounds(10,10,80,30);
    chat.addActionListener(gl);
    contentPane.add(chat);
    this.setVisible(true);
}

private AudioFormat getAudioFormat() {
    float sampleRate = 8000.0F;
    int sampleSizeBits = 16;
    int channels = 1;
    boolean signed = true;
    boolean bigEndian = false;
    //AudioFormat.Encoding.ULAW
    return new AudioFormat(sampleRate, sampleSizeBits, channels, signed, bigEndian);
}

class GUIListener implements ActionListener {

    public void actionPerformed(ActionEvent actionevent) {
        String action = actionevent.getActionCommand();
        switch (action) {
            case "Mute":
                outVoice = false;
                chat.setText("Voice");
                break;
            case "Voice":
                new Thread(osl).start(); 
                outVoice = true;
                chat.setText("Mute");
                break;
        }
    }
}

class IncomingSoundListener implements Runnable {
    @Override
    public void run() {
        try {
            System.out.println("Listening for incoming sound");
            DataLine.Info speakerInfo = new DataLine.Info(SourceDataLine.class, format);
            SourceDataLine speaker = (SourceDataLine) AudioSystem.getLine(speakerInfo);
            speaker.open(format);
            speaker.start();
            while(inVoice) { 
                byte[] data = baos.toByteArray();
                baos.reset();
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                AudioInputStream ais = new AudioInputStream(bais,format,data.length);
                int numBytesRead = 0;
                if ((numBytesRead = ais.read(data)) != -1) speaker.write(data, 0, numBytesRead);
                ais.close();
                bais.close();
            }
            speaker.drain();
            speaker.close();
            System.out.println("Stopped listening for incoming sound");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class OutgoingSoundListener implements Runnable {
    @Override
    public void run() {
        try {
            System.out.println("Listening for outgoing sound");
            DataLine.Info micInfo = new DataLine.Info(TargetDataLine.class, format);
            TargetDataLine mic = (TargetDataLine) AudioSystem.getLine(micInfo);
            mic.open(format);
            byte tmpBuff[] = new byte[mic.getBufferSize()/5];
            mic.start();
            while(outVoice) {
                int count = mic.read(tmpBuff,0,tmpBuff.length);
                if (count > 0) baos.write(tmpBuff, 0, count);
            }
            mic.drain();
            mic.close();
            System.out.println("Stopped listening for outgoing sound");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    new VoiceTest();    
}
}
  • 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-01T02:56:35+00:00Added an answer on June 1, 2026 at 2:56 am

    You should try higher sampling rates and try to find acceptable quality/size ratio for your audio stream.

    Checking the AudioFormat reference is also a good start for getting the idea.

    Try changing local variables in your getAudioFormat() method to this:

    private AudioFormat getAudioFormat() {
        float sampleRate = 16000.0F;
        int sampleSizeBits = 16;
        int channels = 1;
        ...
    }
    

    This is equivalent to a 256 kbps Mono audio file.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have thousands of HTML files to process using Groovy/Java and I need to

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.