How to get the amplitude when blowing into MIC on android device

后端 未结 3 1386
刺人心
刺人心 2021-02-02 05:05

How to get the amplitude when blowing into MIC in android device.

MediaRecorder recorder = new MediaRecorder();
   recorder.setAudioSource(MediaRecorder.AudioSou         


        
3条回答
  •  悲&欢浪女
    2021-02-02 05:32

    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;
    
        }
    

提交回复
热议问题