简体   繁体   中英

Stop recording sound - Java

The code I have written does not stop recording when I hit "console stop". I don't understand what I have done wrong. Could you look at the code below and suggest where my error is?

Here it is:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

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.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

public class Main {

    final static int MONO = 1;
    private static AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;

    public static void main(String[] args) {
        AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                16000, 16, MONO, 2, 16000, true);
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
        int numb = 1;
        String files = "C:\\test\\music\\lala" + numb + "." + fileType;
        File fileOut = new File(files);

        if (!AudioSystem.isLineSupported(info)) {
            System.out.println("Line nit supporot!");
        }
        System.out.println("To stop recording a sound lead - stop");
        Scanner sc = new Scanner(System.in);
        String scc;
        TargetDataLine mike = null;
        try {
            mike = (TargetDataLine) AudioSystem.getLine(info);
            mike.open(format);
            AudioInputStream sound = new AudioInputStream(mike);
            mike.start();
            AudioSystem.write(sound, fileType, fileOut);

            scc = sc.nextLine();
            if (scc.equals("stop")) {
                System.out.println("recording is stopped!");
                mike.stop();
                sound.close();
                System.exit(0);
            }
        } catch (LineUnavailableException e) {
            System.out.println("line not avaible");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

It does not work:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

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.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

public class Main implements Runnable {

    final static int MONO = 1;
    private static AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
    private static TargetDataLine mike;
    private static AudioInputStream sound;

    public static void main(String[] args) {
        AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                16000, 16, MONO, 2, 16000, true);
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
        int numb = 1;
        String files = "C:\\test\\music\\lala" + numb + "." + fileType;
        File fileOut = new File(files);

        if (!AudioSystem.isLineSupported(info)) {
            System.out.println("Line nit supporot!");
        }
        System.out.println("Для остановки записи звука введите - g");
        try {
            mike = (TargetDataLine) AudioSystem.getLine(info);
            mike.open(format);
            sound = new AudioInputStream(mike);
            mike.start();
            AudioSystem.write(sound, fileType, fileOut);

        } catch (LineUnavailableException e) {
            System.out.println("line not avaible");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void run() {
        Scanner sc = new Scanner(System.in);
        String scc;

        scc = sc.nextLine();
        if (scc.equals("g")) {
            System.out.println("Запись звука остановлена");
            mike.stop();
            try {
                sound.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.exit(0);
        }

    }
}

As far a sI can see, your implementation above is missing a major section of code. You seem to be capturing the line (I haven't tried running or debugging the above), but there is nothing showing where you are actually reading data from that line!

If you consult the examples in the Java tutorials, the section in the tutorial "Capturing Audio" titled Reading the Data from the TargetDataLine shows code for this process. Note the use of the boolean "!stopped". This boolean can be used to stop the while loop for the read from an external thread, such as the result of a button press. For best performance, the boolean should be designated volatile, and a public method provided that allows you to set its value to 'false'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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