Search code examples
androidandroid-mediaplayerandroid-mediarecorderandroid-audiorecord

AudioRecorder App crashes when I record a second time


My app breaks down when I attempt to record a second a time when I hit the stop button. I went through this code several times. I can't find the problem. I'm not sure if its my MediaRecorder or my MediaPlayer. It works the first time around. But not the second time. can anyone find the problem.

public class PatientName extends Activity implements OnClickListener {
    private Button instructionsBtn;
    private ImageView record;
    private ImageView stop;
    private MediaPlayer instructions;
    private MediaPlayer namePlayer;
    private MediaRecorder nameRecorder;
    private String OUTPUT_FILE;
    private Button play;
    private Button accept;
    private LinearLayout review;
    private Button delete;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.patient_name);
        OUTPUT_FILE = Environment.getExternalStorageDirectory() + "/namerecording.3gpp";    
        initializeViews();
        initializeListeners();
    }

    public void initializeViews() {
        instructionsBtn = (Button)findViewById(R.id.patient_name_instructions);
        record = (ImageView)findViewById(R.id.record_name);
        stop = (ImageView)findViewById(R.id.stop_name);
        stop.setVisibility(View.GONE);
        play = (Button)findViewById(R.id.play_name); 
        play.setVisibility(View.GONE);
        review = (LinearLayout)findViewById(R.id.review_name);
        delete = (Button)findViewById(R.id.delete_name);
        accept = (Button)findViewById(R.id.accept_name);
        review.setVisibility(View.GONE);
    }

    public void initializeListeners() {
        instructionsBtn.setOnClickListener(this);
        record.setOnClickListener(this);
        stop.setOnClickListener(this);
        play.setOnClickListener(this);
        delete.setOnClickListener(this);
        accept.setOnClickListener(this);
    }

    @Override
    public void onBackPressed() {
    }

    public void playInstructions() {
        setMaxVolume();
        instructions = MediaPlayer.create(PatientName.this, R.raw.intro_instructions);  
        instructions.start();
        instructions.setOnCompletionListener(new OnCompletionListener() {

            public void onCompletion(MediaPlayer play) {
                instructions.release();
                instructionsBtn.setEnabled(true);
            }
        }); 
    }

     public void setMaxVolume() {
        AudioManager audio = (AudioManager) getSystemService(this.AUDIO_SERVICE);
        int maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        audio.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);
    }


    public void onClick(View v) {
        int id = v.getId();
        if (id==R.id.patient_name_instructions) {
            instructionsBtn.setEnabled(false);
            playInstructions();
        }

        if (id==R.id.record_name) {
            beginRecording();
        }

        if (id==R.id.stop_name){
            play.setVisibility(View.VISIBLE);
            nameRecorder.stop();
        }

        if (id==R.id.play_name) {
            playbackRecording();
        }

        if (id==R.id.delete_name){
            deleteRecording();
        }

        if (id==R.id.accept_name) {
            saveAndContinue();
        }

    }

    private void beginRecording(){
        record.setVisibility(View.GONE);
        stop.setVisibility(View.VISIBLE);
        File outFile = new File(OUTPUT_FILE);
        if (outFile.exists()) {
            outFile.delete();
        }else {
            nameRecorder = new MediaRecorder();
            nameRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            nameRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            nameRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
            nameRecorder.setOutputFile(OUTPUT_FILE);
            try {
                nameRecorder.prepare();
            } catch (IllegalStateException e) {
                Toast toast = Toast.makeText(this,"Illegal State",Toast.LENGTH_LONG);
                toast.show();
                e.printStackTrace();
            } catch (IOException e) {
                Toast toast = Toast.makeText(this,"Error Recording",Toast.LENGTH_LONG);
                toast.show();
                e.printStackTrace();
            }
            nameRecorder.start();
        }

    }

    private void playbackRecording() {
        play.setVisibility(View.GONE);
        namePlayer = new MediaPlayer();
        try {
            namePlayer.setDataSource(OUTPUT_FILE);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            namePlayer.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        namePlayer.start();
        namePlayer.setOnCompletionListener(new OnCompletionListener() {

            public void onCompletion(MediaPlayer play) {
                namePlayer.release();
                nameRecorder.release();
                review.setVisibility(View.VISIBLE);
            }
        }); 

    }

    private void deleteRecording() {
        Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
    }

    private void saveAndContinue() {
        Intent intent = new Intent(PatientName.this, PatientLocation.class);
        startActivity(intent);
    }

}

Here is my logCat:

enter image description here


Solution

  • enter image description hereI figured out the issue. In the beginRecording method, I have an if and else statement that handles the output file. Unfortunately, the code that I had in the else statement was something that I wanted to always run no matter what.