我正在尝试重命名使用 MediaRecorder 录制的文件。这就是我接近这个的方式。
首先,我创建了一个 startRecording() 和 stopRecording 方法
public void startRecording (){
recorder = new MediaRecorder();
recorder.reset();
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
externalOutputPath = externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3";
recorder.setOutputFile(externalOutputPath);
}
else
{
storagePath = Environment.getDataDirectory().getAbsolutePath();
recorder.setOutputFile(storagePath + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3");
}
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stopRecording() {
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
}
这就是我开始和停止录制的方式:
recBtn.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn_pressed);
startRecording();
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn);
stopRecording();
nameAlert();
}
return true;
}
});
} //END OF ONCREATE
所以接下来我想做的是能够在每次停止按下录制按钮时设置输出文件的名称。我试图通过创建一个新方法来做到这一点,每次我停止录制时都会调用该方法。
public void nameAlert() {
final EditText etFileName = (EditText) findViewById(com.whizzappseasyvoicenotepad.R.id.etFileName);
Button okBtn = (Button) findViewById(com.whizzappseasyvoicenotepad.R.id.okBtn);
final Dialog dialog = new Dialog (getApplicationContext(), android.R.style.Theme_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(com.whizzappseasyvoicenotepad.R.layout.alert_name);
okBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
fileName = etFileName.getText().toString();
recorder.setOutputFile(externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/" + fileName + ".mp3");
dialog.dismiss();
}
});
}
然后我在调用 stopRecording(); 之后立即添加了这个方法;
现在每次我停止录制(通过停止触摸图像按钮)应用程序都会崩溃。这是 logcat 文件:
08-01 22:39:26.790: E/InputEventReceiver(8258): Exception dispatching input event.
08-01 22:39:26.790: E/MessageQueue-JNI(8258): Exception in MessageQueue callback: handleReceiveCallback
08-01 22:39:26.806: E/MessageQueue-JNI(8258): java.lang.NullPointerException
08-01 22:39:26.806: E/MessageQueue-JNI(8258): at com.whizzappseasyvoicenotepad.MainActivity.nameAlert(MainActivity.java:117)
08-01 22:39:26.806: E/MessageQueue-JNI(8258): at com.whizzappseasyvoicenotepad.MainActivity$3.onTouch(MainActivity.java:59)
08-01 22:39:26.822: E/AndroidRuntime(8258): FATAL EXCEPTION: main
08-01 22:39:26.822: E/AndroidRuntime(8258): java.lang.NullPointerException
08-01 22:39:26.822: E/AndroidRuntime(8258): at com.whizzappseasyvoicenotepad.MainActivity.nameAlert(MainActivity.java:117)
08-01 22:39:26.822: E/AndroidRuntime(8258): at com.whizzappseasyvoicenotepad.MainActivity$3.onTouch(MainActivity.java:59)
它说错误在第 117 行和第 59 行。
第 59 行:
nameAlert();
第 117 行:
okBtn.setOnClickListener(new OnClickListener() {
我仍然是一个初学者程序员所以也许我以错误的方式接近这个,我作为一个程序员的想法还不完美,所以请让我知道我该怎么做。谢谢!
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/dim"
android:padding="30dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="129dp"
android:text="Enter the name of the recorded file:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/etFileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/okBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etFileName"
android:layout_centerHorizontal="true"
android:text="Ok" />
</RelativeLayout>