Reputation: 878
How should we go about fixing this code. We've spent the past 24 hours straight trying to get audio to save in Android
Our tired eyes would much appreciate the help!
We're trying to create an audio analysis app which analyses a microphone input
package com.example.opus2;
import android.content.ContentValues;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
public class audioTest extends MainActivity{
public int saveMarker=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.save_test);
}
public void onClick(View view){
switch (view.getId()){
case R.id.saveBttn: {
Button saveButton= (Button) findViewById(R.id.saveBttn);
if(saveMarker==0){
TextView done = (TextView) findViewById(R.id.done_text);
done.setText("Done2");
saveButton.setText("Saved?");
saveMarker=1;
String save_file = "viola";
try {
recordAudio(save_file);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
else{
saveButton.setText("Save");
saveMarker=0;
break;
}
}
}
}
public void recordAudio(String fileName) throws IOException {
TextView done = (TextView) findViewById(R.id.done_text);
done.setText("Done1");
final MediaRecorder recorder = new MediaRecorder();
ContentValues values = new ContentValues(3);
values.put(MediaStore.MediaColumns.TITLE, fileName);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile("/sdcard/sound/" + fileName+".mp4");
try {
recorder.prepare();
} catch (Exception e){
e.printStackTrace();
}
/* final ProgressDialog mProgressDialog = new ProgressDialog(audioTest.this);
mProgressDialog.setTitle(R.string.lbl_recording);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setButton("Stop recording", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
mProgressDialog.dismiss();
recorder.stop();
recorder.release();
}
});
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface p1) {
recorder.stop();
recorder.release();
}
});
mProgressDialog.show();
*/
recorder.start();
done = (TextView) findViewById(R.id.done_text);
done.setText("Done");
}
}
----ERRORS------
11-02 22:29:18.416 3256-3256/com.example.opus2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3599)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3594)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException
at android.media.MediaRecorder.start(Native Method)
at com.example.opus2.audioTest.recordAudio(audioTest.java:96)
at com.example.opus2.audioTest.onClick(audioTest.java:40)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3594)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 4563
Reputation: 1459
Try to check the state of your Media Recorder if it actually is prepared. If not: wait for some milliseconds until it is prepared before calling the start record.
Also check the reference on android developers for more information on the different recorder states: http://developer.android.com/reference/android/media/MediaRecorder.html
Upvotes: 1