Search code examples
javaandroideclipseandroid-mediarecorder

Trying to rename recorded file with a method


I'm trying to rename files that I recorded with MediaRecorder. This is how I approched this.

First, I created a startRecording() and stopRecording method

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;
    }
}

This is how I start and stop recording:

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

So the next thing I wanted to do is to be able to set the name of the output file each time I stop pressing the record button. I tried to do it by creating a new method that would be called each time I stop recording.

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();
        }
    });
}

I then added this method right after I called stopRecording();

Now everytime I stop recording (by stop touching the imagebutton) the app crashes. This is the logcat file:

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)

It says the error is on line 117 and 59.

Line 59:

nameAlert();

Line 117:

okBtn.setOnClickListener(new OnClickListener() {

I'm still a beginner programmer so maybe I approached this the wrong way, my thinking as a programmer isn't yet perfect, so please let me know how I could do this. Thank you!

Layout file:

<?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>

Solution

  • Unless you really need a specific design for your app, why don't you try using a simple dialog builder? You can add edittext and a button to your dialog programatically and it'll work aswell. Something like this:

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

    Then just set your positive and negative button and you're good.