有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java试图模拟麦克风(javax.sound.sampled)

我正在试着播放麦克风发出的声音。代码成功地用于扬声器,但不用于我的麦克风

我正在使用SourceDataLine&;从文件(.wav)写入。代码仅在我为混音器选择扬声器时有效。如何播放麦克风发出的声音

public void playSound(String fileName, String micName) {
    Mixer mixer = getMixer(micName);
    if (mixer == null) {
        return;
    }
    File file = new File(fileName);
    DataLine.Info info = null;
    SourceDataLine source = null;
    AudioInputStream stream = null;
    byte[] buffer = null;
    try {
        stream = AudioSystem.getAudioInputStream(file);
        System.out.println(stream.getFormat().toString());
        info = new DataLine.Info(SourceDataLine.class, stream.getFormat());
        source = (SourceDataLine) mixer.getLine(info);
        source.open(stream.getFormat());
        buffer = stream.readAllBytes();
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
    source.start();
    source.write(buffer, 0, buffer.length);
    source.drain();
    source.close();
}

private Mixer getMixer(String micName) {
    for (Mixer.Info info: AudioSystem.getMixerInfo()) {
        if (info.getName().equals(micName)) {
            return AudioSystem.getMixer(info);
        }
    }
    return null;
}

当我尝试将此SourceDataLine写入麦克风混音器时,我得到一个

java.lang.IllegalArgumentException (Line unsupported: interface SourceDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian)

但使用扬声器不会造成问题


共 (0) 个答案