我目前正在开发一个简单的语音记录器应用程序android应用程序,现在我想在其中添加其他功能。 谁能帮我做一个录音机,录制2分钟的音频片段。
public void intervalRecorder(long addOfThree){
//This code is not working properly
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mIntervalRecorder.setEnabled(false);
mStopRecordBtn.setEnabled(false);
mRecordBtn.setEnabled(false);
k=1;
testPath=UUID.randomUUID().toString() + "_audio_record.3gp";
pathSave = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Recorder Application/" +testPath;
setupMediaRecorder12();
try {
mediaRecorder12.prepare();
mediaRecorder12.start();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(RecordActivity.this,"Recording...",Toast.LENGTH_SHORT).show();
}
},addOfThree);
mediaRecorder12.stop();
uploadAudio();
Toast.makeText(RecordActivity.this,"Stop...",Toast.LENGTH_SHORT).show();
k=0;
mIntervalRecorder.setEnabled(true);
mStopRecordBtn.setEnabled(true);
mRecordBtn.setEnabled(true);
}
`
private void setupMediaRecorder12() {
mediaRecorder12 = new MediaRecorder();
mediaRecorder12.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder12.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder12.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder12.setOutputFile(pathSave);
}
我不知道为什么我得到NullPointerException
。
Logcat错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaRecorder.stop()' on a null object reference
at com.shashank.recorderapplication.RecordActivity.intervalRecorder(RecordActivity.java:299)
at com.shashank.recorderapplication.RecordActivity$5.onClick(RecordActivity.java:242)
at android.view.View.performClick(View.java:6935)
at android.widget.TextView.performClick(TextView.java:12752)
at android.view.View$PerformClick.run(View.java:26211)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7000)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
答案 0 :(得分:0)
将mediaRecorder12 = new MediaRecorder();
放在setContentView
之后,而不是放在intervalRecorder
函数内部。
答案 1 :(得分:0)
//开始功能必须在处理程序之外,而停止将在处理程序内部
ax.view_init()