title: My mobile app cannot record a call (java)
body: <p>I am developing an app that records system audio and also calls. I am using audiomanager. The problem is that the app fails to record calls, and when there is a call the audio recording stops and resumes only once the call is over. I leave you my code. Would anyone know how to help me?</p>
  <p>I have considered this post here <a href="https://stackoverflow.com/questions/18887636/how-to-record-phone-calls-in-android">How to record phone calls in android?</a> and adapted the code he posted @Mukesh Parmar</p>
  <pre><code>@Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
              rec = findViewById(R.id.btn_rec);
              chronometer = findViewById(R.id.chronometer);
              prgwait = findViewById(R.id.wait);
              stop = findViewById(R.id.btn_stop);
      
              ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_RECORD_AUDIO_PERMISSION);
      
              rec.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
      
                      prgwait.setVisibility(View.VISIBLE); //start progress
                      chronometer.setVisibility(View.VISIBLE);
                      rec.setVisibility(View.GONE);
                      prgwait.setVisibility(View.GONE); //stop progress
                      stop.setVisibility(View.VISIBLE);
                      chronometer.setBase(SystemClock.elapsedRealtime());
                      chronometer.start();
                      Toast.makeText(MainActivity.this, &quot;Registration started&quot;, Toast.LENGTH_SHORT).show();
                      //start rec
                      recorder = new MediaRecorder();
                      recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                      recorder.setOutputFormat(output_formats[currentFormat]);
                      //recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                      recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                      recorder.setOutputFile(getFilename());
                      recorder.setOnErrorListener(errorListener);
                      recorder.setOnInfoListener(infoListener);
      
      
                      try {
                          recorder.prepare();
                          recorder.start();
                      } catch (IllegalStateException e) {
                          Log.e(&quot;REDORDING :: &quot;, e.getMessage());
                          e.printStackTrace();
                      } catch (IOException e) {
                          Log.e(&quot;REDORDING :: &quot;, e.getMessage());
                          e.printStackTrace();
                      }
      
                      audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE); //record the audio of device
                      audioManager.setMode(AudioManager.MODE_IN_CALL);
                      audioManager.setSpeakerphoneOn(true); //enable the speaker for record the voices of the call
      
                      //end code for rec calls
                  }
              });
      
      
              //stop a rec
      
              stop.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
      
                      prgwait.setVisibility(View.VISIBLE);   //start progressbar
                      stop.setVisibility(View.GONE);        
                          chronometer.stop();
                          chronometer.setVisibility(View.GONE);
                          rec.setVisibility(View.VISIBLE);
      
                          audioManager.setSpeakerphoneOn(false); //turn off the speaker
      
                          try {
                              if (null != recorder) {
                                  recorder.stop();
                                  recorder.reset();
                                  recorder.release();
      
                                  recorder = null;
                              }
                          } catch (RuntimeException stopException) {
      
                          }
                          Toast.makeText(MainActivity.this, &quot;Record saved&quot;, Toast.LENGTH_SHORT).show();
                          //end stop code
                          chronometer.setBase(SystemClock.elapsedRealtime());
                          prgwait.setVisibility(View.GONE);                       }
      
              });
  
   private String getFilename() {
          String filepath = Environment.getExternalStorageDirectory().getPath();
          File file = new File(filepath, AUDIO_RECORDER_FOLDER);
  
          if (!file.exists()) {
              file.mkdirs();
          }
  
          return (file.getAbsolutePath() + &quot;/&quot; + System.currentTimeMillis() + file_exts[currentFormat]);
      }
  
      private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
          @Override
          public void onError(MediaRecorder mr, int what, int extra) {
              Toast.makeText(MainActivity.this,
                      &quot;Error: &quot; + what + &quot;, &quot; + extra, Toast.LENGTH_SHORT).show();
          }
      };
  
      private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
          @Override
          public void onInfo(MediaRecorder mr, int what, int extra) {
              Toast.makeText(MainActivity.this,
                      &quot;Warning: &quot; + what + &quot;, &quot; + extra, Toast.LENGTH_SHORT)
                      .show();
          }
      };
  }
  </code></pre>