YOUR
AudioRecord.getMinBufferSize(SAMPPERSEC,channelConfiguration,audioEncoding);
returns -2 value which crashes while allocation of buffer = new
short[buffersizebytes];
Can you Explain the reason?
On Saturday, May 22, 2010 11:23:34 AM UTC+6, BobG wrote:
try this, turn on audio record permission
package com.aiti.vumeter;
//vumeter
//May 21 2010 Bob Gardner at aol.com
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.TextView;
public class vumeter extends Activity {
public AudioRecord audioRecord;
public VuView vuview;
public Timer timer;
public int channelConfiguration =
AudioFormat.CHANNEL_CONFIGURATION_MONO;
public int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
public int mSamplesRead;//how many samples read
public int buffersizebytes;
public int buflen;
public int scrwidth;
public int scrheight;
public int cursamprate;
public final int SAMPPERSEC = 48000; //samp per sec 8000, 11025,
22050 44100 or 48000
public float vin;//input volts
public float iavg; //avg of input volts
public float ipk;//peak input volts
public float isum; //sum of ans of input volts
public float idbavg; //db below 0 of avg input volts
public float idbpk;
public float pixperdb;
public float pixperdiv;
public final float rangedb=60.0f;
public final float dbperdiv=3.0f;
public final float barwid=30f;
public float scrbot;
public long t1,t2,dt, t3,t4,dt2;
public short[] buffer; //+-32767
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
scrwidth=metrics.widthPixels; //
scrheight=metrics.heightPixels; //
scrbot=scrheight-100; //480-100-380
pixperdb=scrbot/rangedb; //380/60-5
pixperdiv=dbperdiv*pixperdb; //3*5-15
buffersizebytes =
AudioRecord.getMinBufferSize(SAMPPERSEC,channelConfiguration,audioEncoding);
//
4096 on ion
buffer = new short[buffersizebytes]; //4096
buflen=buffersizebytes/2;//2048
audioRecord = new
AudioRecord(android.media.MediaRecorder.AudioSource.MIC,SAMPPERSEC,
channelConfiguration, audioEncoding,
buffersizebytes); //constructor
cursamprate=audioRecord.getSampleRate();
vuview = new VuView(this);
setContentView(vuview);
timer = new Timer();
timer.schedule(new TimerTask(){
@Override
public void run(){
TimerMethod();
}
}, 0, 200); //every 200 ms
//trigger(); //grab samples and calc db
}//oncreate
private void TimerMethod(){
//This method is called directly by the timer
//and runs in the same thread as the timer.
//We call the method that will work with the UI
//through the runOnUiThread method.
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable(){
public void run() {
//This method runs in the same thread as the UI.
//Do something to the UI thread here
trigger();
}
};
//--
public void acquire(){
try {
audioRecord.startRecording(); //48khz
} catch (Throwable t) {
// Log.e(AudioRecord, startrecording Failed);
}
//SystemClock.sleep(50L); //one period of 20 hz
mSamplesRead = audioRecord.read(buffer, 0, buflen);
try{
audioRecord.stop();
} catch (Throwable t) {
// Log.e(AudioRecord, stoprecording Failed);
}
}
//---
public void avg(){
int i;
float ftmp;
ipk=0f;
isum=0f;
for(i=0; i buflen; i++){ //2048 samples
vin=(float)buffer[i]; //get a sample
ftmp=Math.abs(vin); //abs of