0

因为在 java 中没有简单的方法来访问系统麦克风和扬声器,所以我尝试自己实现类似的东西。为此,我有一个AudioDevice代表任何设备的类。

它是一个抽象类,定义了音频设备所需的所有方法,并且基于javax.sound.sampled.Mixer. 然后我有了这些类Microphone,它们Speaker从. 如、和。_AudioDeviceAudioDevicetransferData(byte[])open(AudioFormat)isOpen()close()

当我运行我的测试代码时,它似乎可以工作,因为没有发生错误。但由于某种原因transferData(byte[]),我的麦克风没有向阵列写入任何数据。如果我使用预填充字节 [] 并将其用作扬声器的参数transferData(byte[]),则扬声器也不会播放任何声音。

我花了几个小时来搜索导致此问题的错误,并且我还尝试捕获/播放声音,就像这里描述的那样,它在我的计算机上也不起作用。

捕捉 播放

我有一个 Windows Professional N 64 位操作系统,如果这可能是它不工作的原因。该程序使用 Java 8 运行。

这是我想出的代码

音频设备:

public abstract class AudioDevice<E> {

private final static Set<AudioFormat> AUDIO_FORMATS = new HashSet<>();
static {
    final float[] sampleRates = new float[] { 8000.0F, 11025.0F, 16000.0F, 22050.0F, 44100.0F };
    final int[] sampleSizes = new int[] { 8, 16 };
    final int[] channels = new int[] { 1, 2 };

    for (final float sampleRate : sampleRates) {
        for (final int sampleSize : sampleSizes) {
            for (final int channel : channels) {
                final AudioFormat bigEndian_signed = new AudioFormat(sampleRate, sampleSize, channel, true, true);
                final AudioFormat bigEndian_unsigned = new AudioFormat(sampleRate, sampleSize, channel, false,
                        true);
                final AudioFormat littleEndian_signed = new AudioFormat(sampleRate, sampleSize, channel, true,
                        false);
                final AudioFormat littleEndian_unsigned = new AudioFormat(sampleRate, sampleSize, channel, false,
                        false);
                AUDIO_FORMATS.add(littleEndian_unsigned);
                AUDIO_FORMATS.add(bigEndian_unsigned);
                AUDIO_FORMATS.add(bigEndian_signed);
                AUDIO_FORMATS.add(littleEndian_signed);
            }
        }

    }
}

protected final Mixer mixer;
private final Mixer.Info mixerInfo;
private final Class<E> lineType;
private final Set<AudioFormat> supportedFormats = new HashSet<>();

protected AudioFormat currentFormat;
protected E currentDataLine;

public AudioDevice(final Mixer pMixer, final Class<E> pLineType) {
    if (pMixer == null) {
        throw new IllegalArgumentException();
    }
    this.mixer = pMixer;
    this.mixerInfo = pMixer.getMixerInfo();
    this.lineType = pLineType;
    loadSupportedFormats(pLineType);
    if (this.supportedFormats.isEmpty()) {
        throw new IllegalArgumentException();
    }
}

public abstract boolean transferData(final byte[] pBuffer);

public abstract boolean open(final AudioFormat pFormat);

public abstract boolean isOpen();

public abstract boolean close();

public final boolean isUsingFormat(final AudioFormat pFormat) {
    if (isOpen() && this.currentFormat != null) {
        return this.currentFormat.equals(pFormat);
    }
    return false;
}

public final Set<AudioFormat> getSupportedFormats() {
    return this.supportedFormats;
}

public final AudioFormat getCurrentFormat() {
    return this.currentFormat;
}

public final boolean isFormatSupported(final AudioFormat pFormat) {
    if (pFormat != null) {
        final Set<AudioFormat> supportedFormats = getSupportedFormats();
        if (supportedFormats != null) {
            final DataLine.Info info = new Info(this.lineType, pFormat);
            return this.mixer.isLineSupported(info);
        }
    }
    return false;
}

public final String getName() {
    return this.mixerInfo.getName();
}

public final String getDescription() {
    return this.mixerInfo.getDescription();
}

public final String getVendor() {
    return this.mixerInfo.getVendor();
}

public final String getVersion() {
    return this.mixerInfo.getVersion();
}

private final void loadSupportedFormats(final Class<E> pLineType) {
    for (final AudioFormat format : AUDIO_FORMATS) {
        final DataLine.Info info = new Info(pLineType, format);
        if (this.mixer.isLineSupported(info)) {
            this.supportedFormats.add(format);
        }
    }
}
}

麦克风:

public final class Microphone extends AudioDevice<TargetDataLine> {

public Microphone(final Mixer pMixer) {
    super(pMixer, TargetDataLine.class);
}

@Override
public final boolean transferData(final byte[] pBuffer) {
    if (isOpen()) {
        this.currentDataLine.read(pBuffer, 0, pBuffer.length);
        return true;
    }
    return false;
}

@Override
public final boolean open(final AudioFormat pFormat) {
    if (!isOpen() && isFormatSupported(pFormat)) {
        try {
            this.mixer.open();
            final DataLine.Info info = new Info(TargetDataLine.class, pFormat);
            this.currentDataLine = (TargetDataLine) this.mixer.getLine(info);
            this.currentFormat = pFormat;
            return true;
        } catch (final LineUnavailableException e) {
            close();
            return false;
        }
    }
    return false;

}

@Override
public final boolean isOpen() {
    return this.mixer.isOpen() && this.currentDataLine != null;
}

@Override
public final boolean close() {
    if (isOpen()) {
        this.currentDataLine.close();
        this.mixer.close();
        this.currentDataLine = null;
        this.currentFormat = null;
        return true;
    }
    this.currentDataLine = null;
    this.currentFormat = null;
    return false;
}
}

扬声器:

public final class Speaker extends AudioDevice<SourceDataLine> {

public Speaker(final Mixer pMixer) {
    super(pMixer, SourceDataLine.class);
}

@Override
public final boolean transferData(final byte[] pBuffer) {
    if (isOpen()) {
        this.currentDataLine.write(pBuffer, 0, pBuffer.length);
        return true;
    }
    return false;
}

@Override
public final boolean open(final AudioFormat pFormat) {
    if (!isOpen() && isFormatSupported(pFormat)) {
        try {
            this.mixer.open();
            final DataLine.Info info = new Info(SourceDataLine.class, pFormat);
            this.currentDataLine = (SourceDataLine) this.mixer.getLine(info);
            this.currentFormat = pFormat;
            return true;
        } catch (final LineUnavailableException e) {
            close();
            return false;
        }
    }
    return false;

}

@Override
public final boolean isOpen() {
    return this.mixer.isOpen() && this.currentDataLine != null;
}

@Override
public final boolean close() {
    if (isOpen()) {
        this.currentDataLine.close();
        this.mixer.close();
        this.currentDataLine = null;
        this.currentFormat = null;
        return true;
    }
    this.currentDataLine = null;
    this.currentFormat = null;
    return false;
}
}
4

0 回答 0