捕获内部音频Java [英] capturing internal audio java

查看:87
本文介绍了捕获内部音频Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将记录我程序中的声音.我使用Ubuntu 14.04和PulseAudio.现在,我尝试从Pulseaudio进行录音,但目前我仅从麦克风进行录音.如何从Pulseaudio而不是麦克风记录声音?

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

然后:

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

希望任何人都有解决方案.

Hope anyone have a solution.

问候丹尼尔

推荐答案

仅靠Java不能解决此问题.Java仅看到已经存在的设备,如以下Java程序所示:

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.

应该有可能使用 Process 从Java运行那些 pactl 命令.

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

这篇关于捕获内部音频Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆