import java.security.NoSuchAlgorithmException; import java.security.MessageDigest; import java.lang.reflect.*; import java.net.*; import java.io.*; import javax.swing.*; import java.util.regex.*; import java.util.*; import android.media.AudioFormat; import android.media.AudioRecord; import android.media.MediaRecorder; public class main { public static void main(String[] args) throws Exception { SoundMeter sm = new SoundMeter(); sm.start(); for (int i = 0; i < 20; i ++) { Thread.sleep(500); System.out.println(sm.getAmplitude()); } sm.stop(); } } class SoundMeter { private AudioRecord ar = null; private int minSize; public void start() { minSize= AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); ar = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,minSize); ar.startRecording(); } public void stop() { if (ar != null) { ar.stop(); } } public double getAmplitude() { short[] buffer = new short[minSize]; ar.read(buffer, 0, minSize); int max = 0; for (short s : buffer) { if (Math.abs(s) > max) { max = Math.abs(s); } } return max; } }