因为在java中没有简单的方法来访问系统麦克风和扬声器,所以我尝试自己实现类似的东西。为此我有一节课 AudioDevice
代表任何设备。
它是一个抽象类,定义了音频设备所需的所有方法,并基于 javax.sound.sampled.Mixer
. 那我就上课了 Microphone
以及 Speaker
从 AudioDevice
正在实现 AudioDevice
. 例如 transferData(byte[])
, open(AudioFormat)
, isOpen()
以及 close()
.
当我运行我的测试代码,它似乎工作,因为没有错误发生。但由于某种原因 transferData(byte[])
我的麦克风没有向阵列写入任何数据。如果我使用一个带前缀的byte[],并将其用作 transferData(byte[])
对于我的扬声器,扬声器也没有播放任何声音。
我花了好几个小时来搜索导致这个问题的错误,我也试着捕捉/播放这里描述的声音,但在我的电脑上也不起作用。
捕获播放
我有一个WindowsProfessionalN 64位操作系统,如果这可能是它不工作的原因。这个程序是用java8运行的。
这就是我想出的代码
音频设备:
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;
}
}
暂无答案!
目前还没有任何答案,快来回答吧!