Emadi_mehrdad
Emadi_mehrdad

Reputation: 156

Record audio and java.lang.NullPointerException error

I am developing an app that whenever you click on the start recording button that record my audio and when i click on the stop recording button the audio recording stopped and save it in sdcard,myFolder when i click on the start recording I get java.lang.NullPointerException error,the app has stops unexpectedly. How do I fix this? Please help me,im a new android developer.

public class MainActivity extends AppCompatActivity {
        protected static final int request = 0;

        Intent intent;
        private MediaRecorder myRecorder;
        Button btnStopRecord;
        Button btnStarRecord;
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final File dir = new File(Environment.getExternalStorageDirectory() + "/myFolder");
        dir.mkdirs();
        myRecorder = new MediaRecorder();
        myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        btnStarRecord = (Button) findViewById(R.id.StartRecording);
        btnStarRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Date date = new Date();
                final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
                myRecorder.setOutputFile(Environment.getExternalStorageDirectory() + "/myFolder" + "/" + dateFormat.format(date) + ".amr");
                start(v);
            }
        });
        btnStopRecord = (Button) findViewById(R.id.stopRecording);
        btnStopRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stop(v);
            }


        });
    }
private void stop(View v) {
        try {
            myRecorder.stop();
            myRecorder.release();
            myRecorder = null;

            btnStopRecord.setEnabled(false);
            btnStarRecord.setEnabled(true);

            Toast.makeText(getApplicationContext(), "Stop recording...",
                    Toast.LENGTH_SHORT).show();
        } catch (IllegalStateException e) {
            //  it is called before start()
            e.printStackTrace();
        } catch (RuntimeException e) {
            // no valid audio/video data has been received
            e.printStackTrace();
        }

    }


    private void start(View view) {
        try {
            myRecorder.prepare();
            myRecorder.start();

        } catch (IllegalStateException e) {
            // start:it is called before prepare()
            // prepare: it is called after start() or before setOutputFormat()
            e.printStackTrace();
        } catch (IOException e) {
            // prepare() fails
            e.printStackTrace();
        }}

08-25 13:59:54.036 21607-21607/com.example.mehrdad.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at com.example.mehrdad.myapplication.MainActivity$4.onClick(MainActivity.java:165) at android.view.View.performClick(View.java:2408) at android.view.View$PerformClick.run(View.java:8816) at android.os.Handler.handleCallback(Handler.java:587) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4633) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 634

Answers (3)

Neeraj Singh
Neeraj Singh

Reputation: 668

Remove code from onCreateView and Check for this link and update permission.and you dont need to myRecorder =null; on stop recording.

public void startRecording(){
            final Date date = new Date();
            final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
            myRecorder = new MediaRecorder();
            myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
            myRecorder.setOutputFile(Environment.getExternalStorageDirectory() + "/myFolder" + "/" + dateFormat.format(date) + ".amr");
            try {
        myRecorder.prepare();
        myRecorder.start();

    } catch (IllegalStateException e) {
        // start:it is called before prepare()
        // prepare: it is called after start() or before setOutputFormat()
        e.printStackTrace();
    } catch (IOException e) {
        // prepare() fails
        e.printStackTrace();
    }

    }

Upvotes: 1

Pedro Oliveira
Pedro Oliveira

Reputation: 20500

When you call stop you're setting your recorder to null

myRecorder = null;

Then if you call "start" again you're assuming your myRecorder is instantiated, which is not.

My suggestion is to recreate the object when you need to start recording

Edit:

Create a method with

    myRecorder = new MediaRecorder();
    myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);

And call it before you call start again

Upvotes: 2

kswaughs
kswaughs

Reputation: 3087

Move the myRecorder initialization into onClick method.

btnStarRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Date date = new Date();
            final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");

            myRecorder = new MediaRecorder();
            myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);

            myRecorder.setOutputFile(Environment.getExternalStorageDirectory() + "/myFolder" + "/" + dateFormat.format(date) + ".amr");
            start(v);
        }
    });

Upvotes: 1

Related Questions