gpt4 book ai didi

android - 如何在Android设备上吹入MIC时获得振幅

转载 作者:太空狗 更新时间:2023-10-29 15:57:33 26 4
gpt4 key购买 nike

如何在android设备中获取吹入MIC时的振幅。

MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

Timer timer = new Timer();
timer.scheduleAtFixedRate(new RecorderTask(recorder), 0, 1000);

private class RecorderTask extends TimerTask {
private MediaRecorder recorder;

public RecorderTask(MediaRecorder recorder) {
this.recorder = recorder;
}

public void run() {
Log.v("", "amplitude is" + recorder.getMaxAmplitude());
}
}

我收到一个错误:

ERROR/AndroidRuntime(20927): Caused by: java.lang.RuntimeException: setAudioSource failed.
ERROR/AndroidRuntime(20927): at android.media.MediaRecorder.setAudioSource(Native Method)

最佳答案

public boolean isBlowing()
{
boolean recorder=true;

int minSize = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioRecord ar = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,minSize);


short[] buffer = new short[minSize];

ar.startRecording();
while(recorder)
{

ar.read(buffer, 0, minSize);
for (short s : buffer)
{
if (Math.abs(s) > 27000) //DETECT VOLUME (IF I BLOW IN THE MIC)
{
blow_value=Math.abs(s);
System.out.println("Blow Value="+blow_value);
ar.stop();
recorder=false;

return true;

}

}
}
return false;

}

关于android - 如何在Android设备上吹入MIC时获得振幅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4977728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com