android: Detect sound level

前端 未结 3 1666
忘了有多久
忘了有多久 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条回答
  •  猫巷女王i
    2020-11-27 11:37

    If you want to analyse a sample of sound taken directly from the microphone without saving the data in a file, you need to make use of the AudioRecord Object as follows:

    int sampleRate = 8000;
    try {
        bufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT);
        audio = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate,
                AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT, bufferSize);
    } catch (Exception e) {
        android.util.Log.e("TrackingFlow", "Exception", e);
    }
    

    Then you have to start recording when ready:

    audio.startRecording();
    

    Now it's time to start reading samples as follows:

    short[] buffer = new short[bufferSize];
    
        int bufferReadResult = 1;
    
        if (audio != null) {
    
            // Sense the voice...
            bufferReadResult = audio.read(buffer, 0, bufferSize);
            double sumLevel = 0;
            for (int i = 0; i < bufferReadResult; i++) {
                sumLevel += buffer[i];
            }
            lastLevel = Math.abs((sumLevel / bufferReadResult));
    

    The last code combines all the different samples amplitudes and assigns the average to the lastLeveL variable, for more details you can go to this post.

    Regards!

提交回复
热议问题