简体   繁体   中英

Android : Record audio app crash

Everybody,

I'm trying to make a simple audio recording.

The audio is record perfectly when record button is clicked.

I try to record another audio and play it back. It crash when I try to play the 2nd audio.

Can you please help me on fixing this app so that I can record audio multiple time without crashing the app.

Here's the code.

  stop.setEnabled(false);
  play.setEnabled(false);
  outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";;

  myAudioRecorder=new MediaRecorder();
  myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
  myAudioRecorder.setOutputFile(outputFile);

  record.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
        try {
           myAudioRecorder.prepare();
           myAudioRecorder.start();
        }


        catch (IllegalStateException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }

        catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }

        record.setEnabled(false);
        stop.setEnabled(true);

        Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
     }
  });

  stop.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
        myAudioRecorder.stop();
        myAudioRecorder.reset();

        record.setEnabled(true);
        stop.setEnabled(false);
        play.setEnabled(true);

        Toast.makeText(getApplicationContext(), "Audio recorded successfully",Toast.LENGTH_LONG).show();
     }
  });

 play.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) throws IllegalArgumentException,SecurityException,IllegalStateException {
        MediaPlayer m = new MediaPlayer();

        try {
           m.setDataSource(outputFile);
        }

        catch (IOException e) {
           e.printStackTrace();
        }

        try {
           m.prepare();
        }

        catch (IOException e) {
           e.printStackTrace();
        }

        m.start();
        Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show();
     }
  });

Thanks!

You don't seem to enable record button when stop button is clicked. Write

record.setEnabled(true);

in the onClick() method implementation of stop button.

Another case is when stop button is clicked you are setting myAudioRecorder = null . If after this record button is clicked you will get a NullPointerException on this statement

myAudioRecorder.prepare() //exception

Solution will be to remove the statement of setting myAudioRecorder to null in onClick() implementation of stop.

Another problem comes because of this statement myAudioRecorder.release() . You can't get back the previous instance of MediaRecorder once you have released the resource. Either reinitialize myAudioRecorder each time you record or do not release the resource after stop has been clicked. To look at MediaRecorder lifecycle see this.

Here is a reference code you can look and understand. Please catch exceptions wherever required.

//package name
//imports
public class RecordPlayActivity extends AppCompatActivity implements View.OnClickListener {
// declare buttons here
private MediaRecorder myAudioRecorder;
private String recordOutputFile;
private MediaPlayer mediaPlayer;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(null);
    setContentView(R.layout.layout_name);
    //find buttons view by Id here
    record.setOnClickListener(this);
    stop.setOnClickListener(this);
    play.setOnClickListener(this);
    recordOutputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";
    myAudioRecorder = new MediaRecorder();
    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    myAudioRecorder.setOutputFile(recordOutputFile);
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setDataSource(recordOutputFile);
    //Initial condition
    //stop.setEnabled(false);   //not required as stop can be made always enabled
    play.setEnabled(false);
}

@Override
public void onClick(View view){
    switch(view.getId()){
        case R.id.idForRecord:
            myAudioRecorder.prepare();
            myAudioRecorder.start();
            //Recording started
            record.setEnabled(false);
            // don't make play enabled cause you dont want to play
            // and record at same time without stopping record.
            play.setEnabled(false); //required because play can get enabled from stop but it should not remain when recording
            break;
        case R.id.idForStop:
            //if clicked after record
            myAudioRecorder.stop();
            myAudioRecorder.reset();
            //if clicked after play
            if(mediaPlayer.isLooping()) {
                mediaPlayer.stop();
            }
            //recording stopped and saved;
            record.setEnabled(true);
            play.setEnabled(true);
            break;
        case R.id.idForPlay:
            mediaPlayer.prepare();
            mediaPlayer.start();
            //playing
            record.setEnabled(false); // you dont wanna play and record at same time
            break;
        default:
    }
}

}`

Hope these all solves your problem.

It's crashing because outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp" ; directs to top directory od Android OS ...

If you use this path Environment.getExternalStorageDirectory()+File.separator+"sounds" "/recording.3gp" then your recording will be going into one level down directory "sounds" into directory sounds .

App will not crash if you recording is in folder down from top level path ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM