10

使用 Java 是否可以捕获扬声器输出?此输出不是由我的程序生成的,而是由其他正在运行的应用程序生成的。这可以用Java完成还是我需要求助于C/C++?

4

3 回答 3

7

我有一个基于 Java 的应用程序。它使用Java Sound来挖掘流经系统的声音以对其进行跟踪。它在我自己的(基于 Windows 的)机器上运行良好,但在其他一些机器上完全失败。

已经确定,为了让它在这些机器上工作,需要在软件或硬件中进行音频环回(例如,将扬声器“输出”插孔的引线连接到麦克风“输入”插孔)。

由于我真正想做的只是绘制音乐的轨迹,并且我想出了如何在 Java 中播放目标格式 (MP3),因此没有必要进一步追求其他选项。

(而且我还听说 Mac 上的 Java Sound 被严重损坏了,但我从未仔细研究过。)

于 2011-07-10T23:38:56.043 回答
3

在处理操作系统时,Java 并不是最好的工具。如果您需要/想要将它用于此任务,您可能会结束使用 Java Native Interface (JNI),链接到以其他语言(可能是 c/c++)编译的库。

于 2011-07-10T23:22:03.263 回答
0

拿一根AUX 线,连接到HEADPHONE JACK另一端连接到MICROPHONE JACK并运行此代码

https://www.codejava.net/coding/capture-and-record-sound-into-wav-file-with-java-sound-api

 import javax.sound.sampled.*;
    import java.io.*;

public class JavaSoundRecorder {
    // record duration, in milliseconds
    static final long RECORD_TIME = 60000;  // 1 minute

// path of the wav file
File wavFile = new File("E:/Test/RecordAudio.wav");

// format of audio file
AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;

// the line from which audio data is captured
TargetDataLine line;

/**
 * Defines an audio format
 */
AudioFormat getAudioFormat() {
    float sampleRate = 16000;
    int sampleSizeInBits = 8;
    int channels = 2;
    boolean signed = true;
    boolean bigEndian = true;
    AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits,
                                         channels, signed, bigEndian);
    return format;
}

/**
 * Captures the sound and record into a WAV file
 */
void start() {
    try {
        AudioFormat format = getAudioFormat();
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

        // checks if system supports the data line
        if (!AudioSystem.isLineSupported(info)) {
            System.out.println("Line not supported");
            System.exit(0);
        }
        line = (TargetDataLine) AudioSystem.getLine(info);
        line.open(format);
        line.start();   // start capturing

        System.out.println("Start capturing...");

        AudioInputStream ais = new AudioInputStream(line);

        System.out.println("Start recording...");

        // start recording
        AudioSystem.write(ais, fileType, wavFile);

    } catch (LineUnavailableException ex) {
        ex.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

/**
 * Closes the target data line to finish capturing and recording
 */
void finish() {
    line.stop();
    line.close();
    System.out.println("Finished");
}

/**
 * Entry to run the program
 */
public static void main(String[] args) {
    final JavaSoundRecorder recorder = new JavaSoundRecorder();

    // creates a new thread that waits for a specified
    // of time before stopping
    Thread stopper = new Thread(new Runnable() {
        public void run() {
            try {
                Thread.sleep(RECORD_TIME);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            recorder.finish();
        }
    });

    stopper.start();

    // start recording
    recorder.start();
}

}

于 2019-07-10T09:28:29.403 回答