capturing internal audio java

吃可爱长大的小学妹 提交于 2021-02-11 14:00:54

问题


i will record the sound from my programs. I use Ubuntu 14.04 and PulseAudio. Now i try to record from pulseaudio but currently i'm only recording from my microphone. How can i record the sound from pulseaudio instead of my microphone?

public static void captureAudio() {
    try {
        final AudioFormat format = getFormat();
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
        final TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
        line.open(format);
        line.start();
        Runnable runnable = new Runnable() {
            int bufferSize = (int)format.getSampleRate() * format.getFrameSize();
            byte buffer[] = new byte[bufferSize];

            public void run() {
                out = new ByteArrayOutputStream();
                running = true;
                try {
                    while (running) {
                        int count = line.read(buffer, 0, buffer.length);
                        if (count > 0) {
                            out.write(buffer, 0, count);
                        }
                    }
                    out.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        };
        Thread captureThread = new Thread(runnable);
        captureThread.start();
    } catch (LineUnavailableException ex) {
        ex.printStackTrace();
    }
}

I tried some things to change this in my code:

Mixer mixer = AudioSystem.getMixer(null);

And then:

final TargetDataLine line = (TargetDataLine) mixer.getLine(info);

Hope anyone have a solution.

Greetings Daniel


回答1:


This problem cannot be solved from within Java alone. Java sees only the devices which are already there, as the following Java program demonstrates:

import javax.sound.sampled.*;

public class ListDevices {
    public static void main(final String... args) throws Exception {
        for (final Mixer.Info info : AudioSystem.getMixerInfo())
            System.out.format("%s: %s %s %s %s%n", info, info.getName(), info.getVendor(), info.getVersion(), info.getDescription());
    }
}

What you need to do is create a loopback device for your audio system. The following post shows how to do that: https://askubuntu.com/questions/257992/how-can-i-use-pulseaudio-virtual-audio-streams-to-play-music-over-skype The purpose was different, but it should be adaptable for your situation, as your situation seems simpler to me than the situation described in that post.

It should be possible to run those pactl commands from Java using Process.



来源:https://stackoverflow.com/questions/27473522/capturing-internal-audio-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!