I don’t kno why but the start() method throw an error and crash the app:
public class Noise extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.noise);
MediaRecorder recorder=new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try{
recorder.prepare();
}catch(IllegalStateException e){
Log.d("Error",e.toString());
e.printStackTrace();
}catch(IOException e){
Log.d("Error",e.toString());
e.printStackTrace();
}
recorder.start();
Timer timer=new Timer();
timer.scheduleAtFixedRate(new RecorderTask(recorder), 0, 500);
}
private class RecorderTask extends TimerTask{
TextView risultato=(TextView) findViewById(R.id.risultato_recorder);
private MediaRecorder recorder;
public RecorderTask(MediaRecorder recorder){
this.recorder = recorder;
}
public void run(){
runOnUiThread(new Runnable() {
@Override
public void run() {
risultato.setText("" + recorder.getMaxAmplitude());
}
});
}
}
}
if i remove the prepare and the start, it work but return always 0 in the textview.
anyone can help me? this thing make me crazy
this is the logcat: https://dl.dropbox.com/u/16047047/log.txt
and in the phone, it crash.
It seems you miss a
recorder.setOutputFile(PATH_NAME);See the documentation forMediaRecorderAt least, this is what the stack trace tells us:
This is the relevant source code for
MediaRecorder.prepare():(you can read it on grepcode) The exception is thrown if both the
Fileobject and theFileDescriptorarenull. So I don’t think you can useMediaRecorderwithout supplying a file. You can try the tricky/dev/nullbut I don’t know if it works, and can’t test right now