停止录制声音 - Java

时间:2014-03-15 18:48:14

标签: java audio

当我点击" console stop"时,我写的代码并没有停止录制。我不明白我做错了什么。你能看一下下面的代码并建议我的错误在哪里吗?

这是:

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

    }
}

不起作用:

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

    }
}

1 个答案:

答案 0 :(得分:0)

从目前为止我们可以看到,上面的实现缺少一大段代码。你似乎正在捕捉这条线(我还没有尝试过运行或调试上面的内容),但是没有任何显示你实际从那条线读取数据的地方!

如果您参考Java教程中的示例,请参阅教程中的部分"捕获音频"标题为Reading the Data from the TargetDataLine的代码显示了此过程的代码。注意使用boolean"!stopped"。此布尔值可用于停止从外部线程读取的while循环,例如按下按钮的结果。为了获得最佳性能,布尔值应指定为volatile,并提供一个公共方法,允许您将其值设置为' false'。