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 8526901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:24:14+00:00 2026-06-11T08:24:14+00:00

I have a little program to record and playback .wav files. In the GUI

  • 0

I have a little program to record and playback .wav files.
In the GUI class I have the following code for the “stop” button:

private AudioCapture audCap = new AudioCapture();

stopBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            captureBtn.setEnabled(true);
            stopBtn.setEnabled(false);
            playBtn.setEnabled(true);
            audCap.stopCapture = true; // this doesn't work
            audCap.stopPlayback = true; // this does
        }
    }

In the AudioCapture() class I have this code for playback, which stops correctly when stop button is clicked:

class PlayThread extends Thread {
    byte tempBuffer[] = new byte[10000];

    public void run() {

        stopPlayback = false;

        try {
            sourceDataLine.open(audioFormat);
            sourceDataLine.start();

            int cnt;
            while ((cnt = audioInputStream.read(tempBuffer, 0,
                    tempBuffer.length)) != -1 && stopPlayback == false) {
                if (cnt > 0) {
                    sourceDataLine.write(tempBuffer, 0, cnt);
                }
            }
            sourceDataLine.drain();
            sourceDataLine.close();

        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }
}

I also have this code for recording/capturing, which doesn’t stop when the stop button is clicked:

class CaptureThread extends Thread {
    // An arbitrary-size temporary holding buffer
    byte tempBuffer[] = new byte[10000];

    public void run() {

        stopCapture = false;
        // record as wave
        AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
        // takes user input file name and appends filetype
        audioFile = new File(wavName + ".wav");

        try {
            while (!stopCapture) {

                int cnt = targetDataLine.read(tempBuffer, 0,
                        tempBuffer.length);
                if (cnt > 0) {
                    AudioSystem.write(new AudioInputStream(targetDataLine),
                            fileType, audioFile);
                }
            }
            targetDataLine.stop();
            targetDataLine.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

stopCature & stopPlayback are instance variables in the AudioCapture() class.

I’m using Eclipse and tried setting a break point at “while (!stopCapture)” and it never seems to get beyond this.
Does anyone know if there is anything in the code above that would cause the first method to function as expected but the second not to?

-EDIT-
I’ve tried to put a cut down version of the program into an SSCE but it still runs to a couple of hundred lines:

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class audioTest extends JFrame {

private static final long serialVersionUID = 1L;
AudioCapture audCap = new AudioCapture();

public static void main(String[] args) {
    new audioTest();
}

public audioTest() {

    layoutTransporButtons();
    getContentPane().setLayout(new FlowLayout());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(350, 100);
    setVisible(true);
}

public void layoutTransporButtons() {

    final JPanel guiButtonPanel = new JPanel();
    final JButton captureBtn = new JButton("Record");
    final JButton stopBtn = new JButton("Stop");
    final JButton playBtn = new JButton("Playback");
    guiButtonPanel.setLayout(new GridLayout());
    this.add(guiButtonPanel);
    captureBtn.setEnabled(true);
    stopBtn.setEnabled(false);
    playBtn.setEnabled(true);

    // Register anonymous listeners
    captureBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            captureBtn.setEnabled(false);
            stopBtn.setEnabled(true);
            playBtn.setEnabled(false);
            // Capture input data from the microphone
            audCap.captureAudio();
        }
    });
    guiButtonPanel.add(captureBtn);

    stopBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            captureBtn.setEnabled(true);
            stopBtn.setEnabled(false);
            playBtn.setEnabled(true);
            audCap.stopRecordAndPlayback = true;
        }
    });
    guiButtonPanel.add(stopBtn);

    playBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            stopBtn.setEnabled(true);
            audCap.playAudio();
        }
    });
    guiButtonPanel.add(playBtn);
}

class AudioCapture {

    volatile boolean stopRecordAndPlayback = false;
    AudioFormat audioFormat;
    TargetDataLine targetDataLine;
    AudioInputStream audioInputStream;
    SourceDataLine sourceDataLine;
    private String wavName;
    private File audioFile;
    /**
     *  capture audio input from microphone and save as .wav file
     */
    public void captureAudio() {

        wavName = JOptionPane.showInputDialog(null,
                "enter name of file to be recorded:");
        try {
            Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
            // Select an available mixer
            Mixer mixer = AudioSystem.getMixer(mixerInfo[1]);
            // Get everything set up for capture
            audioFormat = getAudioFormat();
            DataLine.Info dataLineInfo = new DataLine.Info(
                    TargetDataLine.class, audioFormat);
            // Get a TargetDataLine on the selected mixer.
            targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
            // Prepare the line for use.
            targetDataLine.open(audioFormat);
            targetDataLine.start();
            // Create a thread to capture the microphone
            Thread captureThread = new CaptureThread();
            captureThread.start();
        } catch (Exception e) {
            System.out.println(e);
            System.exit(0);
        }
    }

    /**
     *  This method plays back the audio data that has
     *  been chosen by the user
     */
    public void playAudio() {
        // add file chooser
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(audioFile);
        int returnVal = chooser.showOpenDialog(chooser);
        // retrieve chosen file
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            // create the file
            audioFile = chooser.getSelectedFile();
        }
        // play chosen file
        try {
            audioInputStream = AudioSystem.getAudioInputStream(audioFile);
            audioFormat = audioInputStream.getFormat();
            DataLine.Info dataLineInfo = new DataLine.Info(
                    SourceDataLine.class, audioFormat);
            sourceDataLine = (SourceDataLine) AudioSystem
                    .getLine(dataLineInfo);
            // Create a thread to play back the data
            new PlayThread().start();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }
    /**
     *  This method creates and returns an AudioFormat object
     */
    private AudioFormat getAudioFormat() {
        float sampleRate = 44100.0F;
        // 8000,11025,16000,22050,44100
        int sampleSizeInBits = 16;
        // 8,16
        int channels = 1;
        // 1,2
        boolean signed = true;
        // true,false
        boolean bigEndian = false;
        // true,false
        return new AudioFormat(sampleRate, sampleSizeInBits, channels,
                signed, bigEndian);
    }

    /**
     *  Inner class to capture data from microphone
     */
    class CaptureThread extends Thread {
        // An arbitrary-size temporary holding buffer
        byte tempBuffer[] = new byte[10000];

        public void run() {
            // reset stopCapture to false
            stopRecordAndPlayback = false;
            // record as wave
            AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
            // take user input file name and append file type
            audioFile = new File(wavName + ".wav");

            try {
                targetDataLine.open(audioFormat);
                targetDataLine.start();
                while (!stopRecordAndPlayback) {
                    AudioSystem.write(new AudioInputStream(targetDataLine),
                            fileType, audioFile);
                }
                targetDataLine.stop();
                targetDataLine.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     *  Inner class to play back the data
     */
    class PlayThread extends Thread {
        byte tempBuffer[] = new byte[10000];

        public void run() {
            // reset stop button
            stopRecordAndPlayback = false;

            try {
                sourceDataLine.open(audioFormat);
                sourceDataLine.start();
                int cnt;
                while ((cnt = audioInputStream.read(tempBuffer, 0,
                        tempBuffer.length)) != -1
                        && stopRecordAndPlayback == false) {
                    if (cnt > 0) {
                        sourceDataLine.write(tempBuffer, 0, cnt);
                    }
                }
                sourceDataLine.drain();
                sourceDataLine.close();
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(0);
            }
        }
    }
}

}

  • 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-11T08:24:16+00:00Added an answer on June 11, 2026 at 8:24 am

    You don’t give all the needed information but a very likely reason is a data race in your program.

    Since you are running things in different threads, you need to use some form of synchronization across threads to make sure the changes you make in one thread are visible in the other.

    Typically, in your case, declaring the boolean variables volatile should be sufficient.

    EDIT

    One possibility is that the condition in your while loop does not get evaluated as often as you think (if at all) – you could add some logging to see what is going on:

    stopBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //your code here
            System.out.println("in actionPerformed: " + stopCatpure);
        }
    }
    
    class CaptureThread extends Thread {
        //same code
                while (!stopCapture) {
                    System.out.println("in while: " + stopCapture);
                    //rest of your code
                }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little console C# program like Class Program { static void main(string
I have a little confusion in the following two statements. The below program is
I have written a little program that deletes a record from the database using
I have written a little program that parses log files of anywhere between a
I have a little gui program that on startup reads data from an Excel
I have my little designer tool (my program). On the left side I have
I have a little program allowing users to type-in some regular expressions. afterwards I
I have a little sketch program that I've managed to throw together using Quartz2D,
i have this little program for showing images on apanel.. but i'm not able
I have created a little program that reads in a Java file and feeds

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.