android: Detect sound level

前端 未结 3 1665
忘了有多久
忘了有多久 2020-11-27 10:52

Using MediaRecorder I capture sound from device\'s microphone. From the sound I get I need only to analyze the sound volume (sound loudness), without saving the

3条回答
  •  感动是毒
    2020-11-27 11:24

    1. Use mRecorder.getMaxAmplitude();

    2. For the analysis of sound without saving all you need is use mRecorder.setOutputFile("/dev/null");

    Here´s an example, I hope this helps

    public class SoundMeter {
    
        private MediaRecorder mRecorder = null;
    
        public void start() {
                if (mRecorder == null) {
                        mRecorder = new MediaRecorder();
                        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        mRecorder.setOutputFile("/dev/null"); 
                        mRecorder.prepare();
                        mRecorder.start();
                }
        }
    
        public void stop() {
                if (mRecorder != null) {
                        mRecorder.stop();       
                        mRecorder.release();
                        mRecorder = null;
                }
        }
    
        public double getAmplitude() {
                if (mRecorder != null)
                        return  mRecorder.getMaxAmplitude();
                else
                        return 0;
    
        }
    }
    

提交回复
热议问题