I need to record audio from an Android device. Start of recording is working fine, but i cannot stop audio recording. I am getting a null pointer exception error. I am accessing start()
and stop()
method from other classes. When I use the stop
method the record is null.
Here is my audio class
private String getFilename(){
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
if(!file.exists()){
file.mkdirs();
}
return (file.getAbsolutePath() + "/Questionnair_" + System.currentTimeMillis() + file_exts[currentFormat]);
}
public String startRecording(){
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
//dbstore(getFilename());
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
AppLog.logString("Error: " + what + ", " + extra);
}
};
public static Context getAppContext()
{
return Audio.context;
}
private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
AppLog.logString("Warning: " + what + ", " + extra);
}
};
public void stopRecording(View v) {
// TODO Auto-generated method stub
System.out.println("stoping");
try {
if (null != recorder) {
System.out.println("recrder is null");
recorder.stop();
recorder.release();
recorder = null;
}
} catch (IllegalStateException e) {
// it is called before start()
e.printStackTrace();
} catch (RuntimeException e) {
// no valid audio/video data has been received
e.printStackTrace();
}
}
Here is the code how i am accessing methods
new Audio().startRecording();
new Audio().stopRecording();
How to fix this issue for stop audio recording
You should do something like: