尝试使用方法重命名录制的文件

时间:2013-08-01 20:52:19

标签: java android eclipse android-mediarecorder

我正在尝试重命名使用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>

1 个答案:

答案 0 :(得分:0)

除非您确实需要针对您的应用的特定设计,为什么不尝试使用简单的对话框构建器?您可以以编程方式将edittext和按钮添加到对话框中,它也可以正常工作。像这样:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your message");
final EditText input = new EditText(this);
builder.setView(input);

然后只需设置你的正面和负面按钮就可以了。