How to know whether a MediaRecorder is in running state or not?

Viewed 10389

I wrote a code to record audio of call conversation using MediaRecorder.

how can i know whether a MediaRecorder is in running state or not, to stop the recording. like

boolean running;
MediaRecorder mr;
//what should i assign to running?        
if(running){
   mr.stop()
}

Above code is just an example.. If you do not understand my question, please tell me.. i will explain clearly with actual code..

What all i want to know is "In which state the MediaRecorder is?" -> recording/released/prepared/initial/etc..

3 Answers
   String flag = "0";


         public void audioRecordStart(){
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
               try {
                    if (flag.equals("1")){
                       // It means recorder is already on recording state.
                    }
                    else{
                       myAudioRecorder.prepare();
                       myAudioRecorder.start();
                       flag = "1";
                    }
                } catch (IllegalStateException ise) {
                    // make something ...
                } catch (IOException ioe) {
                    // make something
                }
                Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
            }
            else {
                getAudioPermission();
            }
        }

        public void audioRecordStop() {
            if (flag.equals("0")){
               // It means recorder is already stopped state.
            }
            else {
                myAudioRecorder.stop();
                myAudioRecorder.release();
                myAudioRecorder = null;
                flag = "0";
                Toast.makeText(getApplicationContext(), "Audio Recorder successfully", Toast.LENGTH_LONG).show();
            }
        }
Related