Code example for AudioRecord

Methods: readstartRecordingstop

0
    } 
	private double measure() throws Issue{ 
		double db = -200;
		if(rate==-1 || minBufferSize<=0)
        	throw new Issue("Failed to get minimum buffer size of microphone"); 
		AudioRecord audioInput = new AudioRecord(MediaRecorder.AudioSource.MIC,
                rate,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT,
                minBufferSize*2);
        short[] buffer = new short[minBufferSize*2];
        audioInput.startRecording();
        int nread = audioInput.read(buffer, 0, buffer.length);
        audioInput.stop();
        if(nread==AudioRecord.ERROR_BAD_VALUE || nread==AudioRecord.ERROR_INVALID_OPERATION)
        	throw new Issue("Check if the application Sound Recorder works.\n"); 
    	db = SignalPower.calculatePowerDb(buffer, 0, buffer.length);
    	if(db==Float.NEGATIVE_INFINITY)
    		throw new Issue("Check if sound is mute because input is empty.\n"); 
		BigDecimal bd = new BigDecimal(db);
		BigDecimal rounded = bd.setScale(2, BigDecimal.ROUND_HALF_UP);