简体   繁体   English

采集喇叭 output in Java

[英]Capturing speaker output in Java

Using Java is it possible to capture the speaker output?使用 Java 是否可以捕获扬声器 output? This output is not being generated by my program but rather by other running applications.这个 output 不是由我的程序生成的,而是由其他正在运行的应用程序生成的。 Can this be done with Java or will I need to resort to C/C++?这可以用 Java 来完成,还是我需要求助于 C/C++?

I had a Java based app.我有一个基于 Java 的应用程序。 that used Java Sound to tap into the sound flowing through the system to make a trace of it.使用Java Sound来挖掘流经系统的声音以对其进行跟踪。 It worked well on my own (Windows based) machine, but failed completely on some others.它在我自己的(基于 Windows 的)机器上运行良好,但在其他一些机器上完全失败。

It was determined that in order to get it working on those machines, would take nothing short of an audio loop-back in either software or hardware (eg connect a lead from the speaker 'out' jack to the microphone 'in' jack).已经确定,为了让它在这些机器上工作,需要在软件或硬件中进行音频环回(例如,将引线从扬声器“输出”插孔连接到麦克风“输入”插孔)。

Since all I really wanted to do was plot the trace for music, and I figured how to play the target format (MP3) in Java, it became unnecessary to pursue the other option further.由于我真正想做的只是 plot 跟踪音乐,并且我想出了如何在 Java 中播放目标格式(MP3),因此没有必要进一步追求其他选项。

(And I also heard that Java Sound on Mac. was horribly broken, but I never looked closely into it.) (而且我还听说 Mac 上的 Java 声音严重损坏,但我从未仔细研究过。)

Java is not the best tool when dealing with the OS. Java 不是处理操作系统的最佳工具。 If you need/want to use it for this task, probably you will end using Java Native Interface (JNI), linking to libraries compiled in other languages (probably c/c++).如果您需要/想要将它用于此任务,您可能会最终使用 Java 本地接口 (JNI),链接到以其他语言编译的库(可能是 c/c++)。

Take an AUX cable , connect to HEADPHONE JACK and other end to MICROPHONE JACK and run this code拿一根AUX 线,连接到HEADPHONE JACK另一端连接到MICROPHONE JACK并运行此代码

https://www.codejava.net/coding/capture-and-record-sound-into-wav-file-with-java-sound-api 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();
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM