【问题标题】:Capturing speaker output in Java在 Java 中捕获扬声器输出
【发布时间】:2011-10-02 10:18:05
【问题描述】:

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

【问题讨论】:

  • 无论您使用什么语言,您都必须首先找到可以为您执行此操作的库。然后,只需为此库查找/生成正确的语言绑定即可。
  • 我相信你只能用某些硬件做到这一点。我知道一些 Soundblaster live 卡支持这一点,但其他基本卡不支持(尤其是更糟糕的板载卡)。
  • @casablanca:“你必须首先找到一个可以为你做这件事的库。” 不是 Java。 Java 从 1.3 版本开始就有 Java Sound API。如果 Java Sound 无法完成这项工作(请参阅我的回答),那么 any(基于 Java)库不太可能成功,因为它在逻辑上将基于 Java Sound API。
  • @Andrew Thompson:我在暗示 JNI 的思路,因为显然没有纯基于 Java 的方法来执行此操作。当然,这会破坏可移植性,但这是在 Java 中实现它的唯一方法。

标签: java audio capture javasound


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 为什么是Java not the best tool when dealing with OS
    • 因为 VM 是对可以访问的内容的限制,并且因为它旨在允许平台之间的兼容性,所以它没有详细说明:您不能编辑 Windows 注册表或使用共享内存,发送在 GPU 上运行的代码......不是你不能这样做,而是你需要添加一个使用 JNI 用其他语言编写的外部库。
    • @SJuan76 you cannot edit Windows registry 我很惊讶地发现这是错误的。 See herebut you need to add an external library written in other language using JNI. LWJGL 允许在 GPU 上 执行 OpenGL、-CL、AL 等,而无需进行 JNI/JNA 调用。
    【解决方案3】:

    使用 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();
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2017-12-09
      • 2019-06-17
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多