Java Sound API编码我的WAV文件错误? (在MacOS上)

发布于 2025-01-31 04:22:03 字数 2361 浏览 7 评论 0原文

我正在尝试从麦克风中录制声音,然后将其倒入WAV文件中。

我尝试了几个教程中的代码。当我将AudioInputStream写入WAV文件时,我的程序确实会创建WAV文件。当

我在文件浏览器中查看时,它是正确的时间,帧速率,并且确实具有大小(不是空)。但是当我播放WAV文件时,没有声音,它的空白。

我已经验证了我正在使用的targetDataline确实是内置的麦克风。我已经尝试关闭除终端之外的所有其他应用程序,以确保其他任何其他应用程序都可能打开麦克风作为资源,并且我尝试包装程序并将其从默认的Mac终端运行,以确保我有权访问访问权限资源。

我使用Javase 11的MacOS 12.3.1。

我已经为此做好了充分的准备,因为这是我的计算机或其他东西的问题,因为我使用的教程代码似乎对其他所有人都有用。

这是我的代码:


    public class AudioVisualiser {

    TargetDataLine targetLine;

    public AudioVisualiser() {
        initialise();
    }

    public void initialise() {
        AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
        
        try {

            //Microphone
            TargetDataLine targetLine = (TargetDataLine) AudioSystem.getLine(new DataLine.Info(TargetDataLine.class, format));
            targetLine.open();

            System.out.println(targetLine.available());

            Thread monitorThread = new Thread() {
                @Override
                public void run() {
                    targetLine.start();
                    File outputFile = new File("recording.wav");

                    AudioInputStream stream = new AudioInputStream(targetLine);
                    int written = 0;
                    try {written = AudioSystem.write(stream, AudioFileFormat.Type.WAVE, outputFile);}
                    catch (IOException e) {throw new Error("Problem writing to file");}
                    try {stream.close();} catch (IOException e) {e.printStackTrace();}
                    System.out.println("written bytes = "+written);
                }
            };


            System.out.println("Start Monitor");
            monitorThread.start();

            Thread.sleep(3000);

            System.out.println(targetLine.available());
            targetLine.stop();
            targetLine.close();
            System.out.println("End Monitor");

        }
        catch(InterruptedException ie) { ie.printStackTrace();}
        catch (LineUnavailableException e1) {System.out.println("Problem opening line");}

    }


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

这是创建WAV文件的图片。它的大小,但只是空白。 “在此处输入图像说明”

I'm trying to record sound from my microphone and simply dump it into a wav file.

I have tried code from several tutorials. When I write the AudioInputStream to a wav file my program does create the wav file. When

When I look in the file browser it is the correct length of time, frame rate, and it does have a size (is not empty). But when I play the wav file, there is no sound, its blank.

I have verified that the TargetDataLine I'm using is indeed the built-in microphone. I have tried closing all other applications apart from the terminal to make sure nothing else might possibly have the microphone open as a resource, and I have tried packaging the program and running it from the default mac terminal, to make sure I had permission to access the resource.

I am on a macOS 12.3.1, using JavaSE 11.

I am fully prepared for this to be an issue with my computer or something, because the tutorial code I've been using seems to work for everyone else.

Here is my code:


    public class AudioVisualiser {

    TargetDataLine targetLine;

    public AudioVisualiser() {
        initialise();
    }

    public void initialise() {
        AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
        
        try {

            //Microphone
            TargetDataLine targetLine = (TargetDataLine) AudioSystem.getLine(new DataLine.Info(TargetDataLine.class, format));
            targetLine.open();

            System.out.println(targetLine.available());

            Thread monitorThread = new Thread() {
                @Override
                public void run() {
                    targetLine.start();
                    File outputFile = new File("recording.wav");

                    AudioInputStream stream = new AudioInputStream(targetLine);
                    int written = 0;
                    try {written = AudioSystem.write(stream, AudioFileFormat.Type.WAVE, outputFile);}
                    catch (IOException e) {throw new Error("Problem writing to file");}
                    try {stream.close();} catch (IOException e) {e.printStackTrace();}
                    System.out.println("written bytes = "+written);
                }
            };


            System.out.println("Start Monitor");
            monitorThread.start();

            Thread.sleep(3000);

            System.out.println(targetLine.available());
            targetLine.stop();
            targetLine.close();
            System.out.println("End Monitor");

        }
        catch(InterruptedException ie) { ie.printStackTrace();}
        catch (LineUnavailableException e1) {System.out.println("Problem opening line");}

    }


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

Here is a picture of the created wav file. It has a size, but it is just blank.
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

メ斷腸人バ 2025-02-07 04:22:03

我发现了几个最近的线程(自2020年以来)与访问Mac上麦克风的问题有关。但是我无法判断问题是否真的解决了。这些线程都建议尝试Java 17.0.2或更高版本。

请求访问麦克风

如何请求麦克风访问

我有窗口和Linux计算机,没有Mac,因此我无法测试问题是否继续发布17.0.2。

I've found a couple recent threads (since 2020) pertaining to the issue of accessing the microphone on a Mac. But I can't tell if the issue was actually solved or not. The threads both suggest trying Java 17.0.2 or later.

Request to Access Microphone

How to Request Microphone Access

I have a Windows and a Linux computer, no Mac, so I am unable to test if the problem continues post 17.0.2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文