简体   繁体   English

停止录制声音-Java

[英]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. 如果您查阅Java教程中的示例,则教程“捕获音频”中标题为从TargetDataLine读取数据的部分显示了此过程的代码。 Note the use of the boolean "!stopped". 注意使用布尔值“!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. 该布尔值可用于停止while循环,以便从外部线程进行读取,例如按下按钮的结果。 For best performance, the boolean should be designated volatile, and a public method provided that allows you to set its value to 'false'. 为了获得最佳性能,应将布尔值指定为volatile,并提供一个公共方法,该方法允许您将其值设置为“ false”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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