I want to check the loudness of voice in android

后端 未结 1 1748
遥遥无期
遥遥无期 2021-02-04 15:56

I am developing an application where I have to detect the loudness of voice. Means some on shouts on the mobile and it will show the loudness level on the screen.

Which

1条回答
  •  别跟我提以往
    2021-02-04 16:51

    Look at http://developer.android.com/reference/android/media/AudioRecord.html

    When you read the buffer the byte values will represent the amplitude. The higher the byte value the louder the sound.

    Here is a scaled-down version of something I used in an app I wrote a while back:

    Add this to your mainifest.xml

    soundlevel.xml

    
    
        
    
    
        
    
    
    

    SoundLevel.java

    import android.app.Activity;
    import android.media.AudioFormat;
    import android.media.AudioRecord;
    import android.media.MediaRecorder;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.SystemClock;
    import android.widget.CompoundButton;
    import android.widget.ProgressBar;
    import android.widget.ToggleButton;
    
    public class SoundLevel extends Activity {
    
        private static final int sampleRate = 11025;
        private static final int bufferSizeFactor = 10; 
    
        private AudioRecord audio;
        private int bufferSize;
    
        private ProgressBar level;
    
        private Handler handler = new Handler();
    
        private int lastLevel = 0;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.soundlevel);
    
            level = (ProgressBar) findViewById(R.id.progressbar_level);
    
            level.setMax(32676);
    
            ToggleButton record = (ToggleButton) findViewById(R.id.togglebutton_record);
    
            record.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub
    
                    if (isChecked) {
                        bufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT) * bufferSizeFactor; 
    
                        audio = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
    
                        audio.startRecording();
    
                        Thread thread = new Thread(new Runnable() {
                            public void run() {
                                readAudioBuffer();
                            }     
                          });
    
                        thread.setPriority(Thread.currentThread().getThreadGroup().getMaxPriority());
    
                        thread.start();
    
                        handler.removeCallbacks(update);
                        handler.postDelayed(update, 25);
    
                    } else if (audio != null) {
                        audio.stop();
                        audio.release();
                        audio = null;
                        handler.removeCallbacks(update);
                    }
    
                }
            });
        }
    
        private void readAudioBuffer() {
    
            try {
                short[] buffer = new short[bufferSize];
    
                int bufferReadResult;
    
                do {
    
                    bufferReadResult = audio.read(buffer, 0, bufferSize);
    
                    for (int i = 0; i < bufferReadResult; i++){
    
                        if (buffer[i] > lastLevel) {
                            lastLevel = buffer[i];  
                        }
    
                    }
    
                } while (bufferReadResult > 0 && audio.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING);
    
                if (audio != null) {
                    audio.release();
                    audio = null;
                    handler.removeCallbacks(update);
    
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        private Runnable update = new Runnable() {
    
            public void run() {
    
                SoundLevel.this.level.setProgress(lastLevel);
    
                lastLevel *= .5;
    
                handler.postAtTime(this, SystemClock.uptimeMillis() + 500);
    
            }
    
        };
    }
    

    0 讨论(0)
提交回复
热议问题