1

I just have only basic knowledge about android. today i developed one audio recording application, but the problem is user can record audio , stop the recording and it can play the audio,but after playing when the user press the record button again the app is crashing , anyone can help me?

this is my main activity

package com.hackerinside.jaisonjoseph.sample_recorder;

   import android.media.MediaPlayer;
   import android.media.MediaRecorder;
   import android.os.Bundle;
   import android.os.Environment;
   import android.provider.MediaStore;
   import android.support.design.widget.FloatingActionButton;
   import android.support.design.widget.Snackbar;
   import android.support.v7.app.AppCompatActivity;
   import android.support.v7.widget.Toolbar;
   import android.view.View;
   import android.view.Menu;
   import android.view.MenuItem;
   import android.widget.Button;
   import android.widget.Toast;

    import java.io.IOException;

public class MainActivity extends AppCompatActivity {

private Button play,stop,record;
private MediaRecorder myAudioRecorder;
private String outputfile;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    play=(Button)findViewById(R.id.play);
    stop=(Button)findViewById(R.id.stop);
    record=(Button)findViewById(R.id.record);
    stop.setEnabled(false);
    play.setEnabled(false);


  outputfile= Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording_"+System.currentTimeMillis()+".mp3";



    myAudioRecorder=new MediaRecorder();
    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.DEFAULT);
    myAudioRecorder.setOutputFile(outputfile);

 record.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {

         try {

             myAudioRecorder.prepare();
             myAudioRecorder.start();
     }
     catch (IllegalStateException ise){



     }catch (IOException ioe){



         }
            record.setEnabled(false);
            stop.setEnabled(true);
            Toast.makeText(getApplicationContext(),"record startded",Toast.LENGTH_LONG).show();

     }

 });





    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {



            myAudioRecorder.stop();
            record.setEnabled(true);
            myAudioRecorder.release();
            myAudioRecorder=null;
            stop.setEnabled(false);
            play.setEnabled(true);
            Toast.makeText(getApplicationContext(),"recorded audio",Toast.LENGTH_LONG).show();

        }

    });
            play.setOnClickListener(new View.OnClickListener() {
                 @Override
                public void onClick(View view) {
                     MediaPlayer mediaPlayer=new MediaPlayer();

                     try {
                             mediaPlayer.setDataSource(outputfile);
                             mediaPlayer.prepare();
                             mediaPlayer.start();
                             Toast.makeText(getApplicationContext(),"playing  audio",Toast.LENGTH_LONG).show();
    }
    catch (Exception e)
    {
 // here
    }

}
   });








    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;











}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jaison_Joseph
  • 333
  • 7
  • 26
  • 1
    Can u tell me the solution instead finding the copies? – Jaison_Joseph Mar 04 '17 at 10:39
  • Your question is missing the LogCat and you have posted a lot of unneeded code as well. Posting only the part of code which is relevant can help you pull more people helping you. Please read [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Sufian Mar 04 '17 at 11:04

3 Answers3

4

Try adding this

myAudioRecorder=new MediaRecorder();

in your

record.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {

     try { 
         myAudioRecorder=new MediaRecorder();
         myAudioRecorder.prepare();
         myAudioRecorder.start();
 }

Alternatively u can remove myAudioRecorder.release(); from stop button click.

it should be released during onPause() on your activity

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
2

That is because, you are setting it null while stopping mediaRecorder

stop.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        myAudioRecorder.stop();
        record.setEnabled(true);
        myAudioRecorder.release();
        myAudioRecorder=null;
        stop.setEnabled(false);
        play.setEnabled(true);
        Toast.makeText(getApplicationContext(),"recorded audio",Toast.LENGTH_LONG).show();
    }

});

When you click again on record this will got executed,

    record.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {

         try {
             // myAudioRecorder is null when you come up second time.
             // Initialize myAudioRecorder to prevent an exception.
             myAudioRecorder.prepare();
             myAudioRecorder.start();
         } catch (IllegalStateException ise){

         } catch (IOException ioe) {

         }
         record.setEnabled(false);
         stop.setEnabled(true);
         Toast.makeText(getApplicationContext(),"record startded",Toast.LENGTH_LONG).show();
     }
 });

myAudioRecorder.prepare(); will through NullPointerException as myAudioRecorder is null.
Although above code is in try.. catch but you did not catch NullPointerException, App will crash.

Paresh P.
  • 6,677
  • 1
  • 14
  • 26
0

if you look at documentations it says you don't have access to recorder after you releasd it which is what you are doing when you press stop,you have to instantiate new recorder or simply don't release it.

Farid
  • 1,024
  • 9
  • 16