我需要录制来自Android设备的音频。录制开始工作正常,但我无法停止录音。我收到一个空指针异常错误。我从其他类访问start()
和stop()
方法。当我使用stop
方法时,记录为空。
这是我的音频课
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();
}
}
以下是我如何访问方法的代码
new Audio().startRecording();
new Audio().stopRecording();
如何解决停止录音的问题
答案 0 :(得分:1)
您应该执行以下操作:
2015-06-10 10:39:57.552 INFO 7032 --- [ main] o.s.b.c.e.ServletRegistrationBean : Servlet dispatcherServlet was not registered (disabled)
2015-06-10 10:39:57.553 INFO 7032 --- [ main] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]