12

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..

Jonas
  • 97,987
  • 90
  • 271
  • 355
manidhar mulaparthi
  • 1,052
  • 2
  • 18
  • 30
  • Even though this question is a decade old, the problem still exists, i.e. there is no way to find out if the ```MediaRecorder``` has started recording. However, I just posted a workaround for it here: https://stackoverflow.com/a/66821059/15389960 Posting the link here because I found this question while researching the problem and the accepted solution sent me for a toss, because it was for ```MediaPlayer``` and not for ```MediaRecorder```. Regards. – Tarique Ali Mirza Mar 26 '21 at 16:53

2 Answers2

10

You cannot get the state directly, see the open enhancement request at http://code.google.com/p/android/issues/detail?id=800

You need to set a variable manually in the listeners when the mediaplayer reaches a certain state in order to remember the current state.

Also this this discussion: http://www.mail-archive.com/android-developers@googlegroups.com/msg35320.html

Mathias Conradt
  • 27,904
  • 20
  • 132
  • 186
  • This enhancement request is for MediaPlayer not MediaRecorder – Vivek Oct 05 '18 at 04:26
  • 3
    Android at its *best* again – Manuel May 28 '19 at 14:58
  • So basically you can't even manually implement exact recording status, e.g.: __Sets the maximum duration (in ms) of the recording session. Call this after setOutFormat() but before prepare(). After recording reaches the specified duration, a notification will be sent to the MediaRecorder.OnInfoListener with a "what" code of MEDIA_RECORDER_INFO_MAX_DURATION_REACHED and recording will be stopped. Stopping happens asynchronously, there is no guarantee that the recorder will have stopped by the time the listener is notified..__ – user924 Jan 07 '20 at 18:02
  • You said "set a variable manually in the listeners when the mediaplayer reaches a certain state". What "listeners" did you mean? `MediaRecorder` fires very few events, i.e. info listener, error listener and routing change listened. Which one to listen to know it reaches target state? – Dims Jul 14 '20 at 12:21
0
   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();
            }
        }
  • 1
    Please add some explanation to your answer such that others can learn from it – Nico Haase Apr 12 '20 at 12:32
  • There is no such a method to know the MediaRecorder state. I just gave a example how could u check the state using create your own method like (flag==1) – Manoj Asya Apr 13 '20 at 12:22