samedi 11 avril 2020

When he clicks the button to check whether the checkbox is checked or not and then to record

How to make sure when the checkbox is checked that it does not start recording until the button is clicked. So you have to check and click the button to start recording, if it is not checked on checkbox and click the button is not supposed to record audio. What I did, if the checkbox was checked, start recording immediately.

 toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked) {

            if (isChecked) {

                //Start Recording Audio

                chkIos.isChecked();
                // isChecked = true;

            } else {

                //Stop Recording Audio
                onStop();
                // isChecked = false;

            }

        }
    });


public void addListenerOnChkIos() {

    chkIos = (CheckBox) findViewById(R.id.SaveAudio);

    chkIos.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //is chkIos checked?
            if (((CheckBox) v).isChecked()) {
                startRecording();
                Toast.makeText(VoiceRecognitionActivity.this,
                        "My Message", Toast.LENGTH_LONG).show();

            }

        }
    });

}

private void stopRecording() {

    //Stop media recorder and set it to null for further use to record new audio
    mediaRecorder.stop();
    mediaRecorder.release();
    mediaRecorder = null;
}

private void startRecording() {

    //Get app external directory path
    String recordPath = getBaseContext().getExternalFilesDir("/").getAbsolutePath();

    //initialize filename variable with date and time at the end to ensure the new file wont overwrite previous file
    recordFile = "File" + ".mp3";


    //Setup Media Recorder for recording
    mediaRecorder = new MediaRecorder();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mediaRecorder.setOutputFile(recordPath + "/" + recordFile);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

    try {
        mediaRecorder.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //Start Recording
    mediaRecorder.start();
}

@Override
public void onStop() {
    super.onStop();
    if(isRecording){
        stopRecording();
    }
}



Aucun commentaire:

Enregistrer un commentaire