Monday, November 25, 2013

MediaRecorder crash [android help]


MediaRecorder crash




Very beginner in Android/Java, coming from C and Symbian, I started from the Hellworld example to try to display the sound level (the project is to record this level in a text file for hours). This code is written in the MainActivity file, as a method inside the MainActivity Class :



public void GetAFlevel(View view) throws IllegalStateException, IOException {
float audiolevel = 0, maxal = 0;
MediaRecorder mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile("/dev/null");
mRecorder.prepare();
mRecorder.start();
for (int i = 0; i < 1000; i++) {
audiolevel = mRecorder.getMaxAmplitude();
if (audiolevel > maxal)
maxal = audiolevel;
}
EditText editText1 = (EditText) findViewById(R.id.editText1);
editText1.setText(String.valueOf(maxal));
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}


The result is a crash of the application on the target :



11-17 00:23:09.859: D/AndroidRuntime(22096): Shutting down VM

11-17 00:23:09.859: W/dalvikvm(22096): threadid=1: thread exiting with uncaught exception (group=0x40018578)

11-17 00:23:09.921: E/AndroidRuntime(22096): FATAL EXCEPTION: main

11-17 00:23:09.921: E/AndroidRuntime(22096): java.lang.IllegalStateException: Could not execute method of the activity


No crash if I remove the following MediaRecorder functions : setAudioSource, setOutputFormat, setAudioEncoder, setOutputFile, prepare, start and stop, but getMaxAmplitude returns 0, of course.


As soon as I add only setAudioSource (normaly the only necessary for getMaxAmplitude), crash !


Any idea ?


Thanks in advance.



Read more

stackoverflow.comm



No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...