使用的是TarsosDSP开源库
1.网上下载TarsosDSP.jar导入
2.检测麦克风是否有声音 info.getInteger("sampleRateInHz")跟初始化TarsosDSP的采样率一样即可
//初始化TarsosDSP 采样率,位深度,通道数,编码方式,表示音频的字节序为大端字节序
TarsosDSPAudioFormat audioFormat = new TarsosDSPAudioFormat(16000,16,1,true,false);
TarsosDSPAudioFloatConverter audioConverter = TarsosDSPAudioFloatConverter.getConverter(audioFormat);
SilenceDetector silenceDetector = new SilenceDetector();
//获取麦克风数据大小
recordBufsize = AudioRecord
.getMinBufferSize(info.getInteger("sampleRateInHz"),
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
recordBufsize = Math.max(recordBufsize, 4096); //值越大VAD检测越准
//初始化麦克风
audioRecord = new AudioRecord(IetUtils.audioSource(),
info.getInteger("sampleRateInHz"),
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
recordBufsize);
audioRecord.startRecording();//开始收音
byte data[] = new byte[recordBufsize];
int read;
//recordBufsize byte[]值越大越准 噪音会影响到VAD的检测
while (isRecording) { //这个循环根据实际需要的间隔来检测
read = audioRecord.read(data, 0, recordBufsize); //读取麦克风数据
if (AudioRecord.ERROR_INVALID_OPERATION != read) {
float[] floatData = new float[data.length / audioFormat.getFrameSize()];
audioConverter.toFloatArray(data, floatData);
boolean silence = silenceDetector.isSilence(floatData.clone());
if(silence){
//没声音业务
}else{
//有声音业务
}
}
}
以上就是简单的麦克风检测是否有声音