【问题标题】:Simulate microphone Input模拟麦克风输入
【发布时间】:2014-02-07 05:01:04
【问题描述】:

我正在尝试编写一个小程序来读取 wav 文件并发送输出,就好像它来自我的麦克风一样。不幸的是,我对声音 API 没有太多经验。

背景:我基本上想实现的是一个在我处于语音聊天时播放声音的程序(即 Teamspeak、Ventrilo)。为了让它现在工作,我必须将录音设备切换到“你所听到的”,播放声音,然后切换回麦克风。该程序应模拟来自麦克风的输入。

到目前为止,我只能播放声音。我想我只是需要一个不同的 SourceLine?

public class Player {
private final int BUFFER_SIZE = 128000;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;

public void playSound(File soundFile) {
    try {
        audioStream = AudioSystem.getAudioInputStream(soundFile);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
    audioFormat = audioStream.getFormat();
    DataLine.Info infoIn = new DataLine.Info(SourceDataLine.class,
            audioFormat);
    try {
        sourceLine = (SourceDataLine) AudioSystem.getLine(infoIn);
        sourceLine.open(audioFormat);
    } catch (LineUnavailableException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }

    sourceLine.start();

    int nBytesRead = 0;
    byte[] abData = new byte[BUFFER_SIZE];
    while (nBytesRead != -1) {
        try {
            nBytesRead = audioStream.read(abData, 0, abData.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (nBytesRead >= 0) {
            @SuppressWarnings("unused")
            int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
        }
    }
    sourceLine.drain();
    sourceLine.close();
}
 }

解决方案: 安装 VB 音频线 (http://vb-audio.pagesperso-orange.fr/Cable/)。是捐赠软件。制作VB-Output标准录音设备。在麦克风属性中选择 VB-Input 作为播放。

public class Player {
private final int BUFFER_SIZE = 128000;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;

public void playSound(File soundFile) {
    try {
        audioStream = AudioSystem.getAudioInputStream(soundFile);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
    audioFormat = audioStream.getFormat();
    DataLine.Info infoIn = new DataLine.Info(SourceDataLine.class,
            audioFormat);
    try {
        Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
        Mixer mixer = null;
        for (int i = 0; i < mixerInfos.length; i++) {
            System.out.println(mixerInfos[i].getName());
            if (mixerInfos[i].getName().equals(
                    "CABLE Input (VB-Audio Virtual Cable)")) {
                mixer = AudioSystem.getMixer(mixerInfos[i]);
                break;
            }
        }
        sourceLine = (SourceDataLine) mixer.getLine(infoIn);
        sourceLine.open(audioFormat);
    } catch (LineUnavailableException e) {
        e.printStackTrace();
        System.exit(1);
    }
    sourceLine.start();
    int nBytesRead = 0;
    byte[] abData = new byte[BUFFER_SIZE];
    while (nBytesRead != -1) {
        try {
            nBytesRead = audioStream.read(abData, 0, abData.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (nBytesRead >= 0) {
            @SuppressWarnings("unused")
            int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
        }
    }
    sourceLine.drain();
    sourceLine.close();
}
}

【问题讨论】:

  • 我很困惑; “好像它来自我的麦克风”是什么意思?

标签: java microphone javasound


【解决方案1】:

您可以安装一些程序,例如虚拟音频电缆,并从您的音乐播放器将播放的声音重定向到虚拟输入。在您的程序中,您必须收听该虚拟输入源,并且您可以测试重定向到正常耳机或扬声器接收到的信号或将其保存到文件中。

【讨论】:

  • 是的,这行得通。我必须安装 VoiceMeeter。那与您提到的虚拟音频电缆来自同一个人。我将使用最终解决方案编辑我上面的问题。我对获取线路信息的方式不满意。如果您知道如何调用混音器而不是遍历所有混音器,是否有不同的方法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-09
  • 1970-01-01
  • 2011-08-20
  • 2012-08-14
  • 1970-01-01
  • 1970-01-01
  • 2023-01-31
相关资源
最近更新 更多