title: How to make Call recorder in android studio
body: <p>I made a call recorder that works sometimes but sometimes it doesn't work, I searched about it but I can't solve this problem, here is my code:</p>
  <pre><code>public class RecordingService extends Service {
  
      private MediaRecorder rec;
      private boolean recorderstarted;
      private File file;
      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
  
          return null;
      }
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
          file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS);
  
          Date date = new Date();
          CharSequence sdf = DateFormat.format(&quot;MM-dd-yy-hh-mm-ss&quot;, date.getTime());
  
          rec = new MediaRecorder();
          String manufacturer = Build.MANUFACTURER;
          rec.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
          rec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
          rec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
          rec.setOutputFile(file.getAbsolutePath() + &quot;/&quot; + sdf + &quot;rec.3gp&quot;);
  
          MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
              public void onError(MediaRecorder arg0, int arg1, int arg2) {
                  Toast.makeText(getApplicationContext(), &quot;Crashed&quot;, Toast.LENGTH_SHORT).show();
              }
          };
  
          rec.setOnErrorListener(errorListener);
  
          TelephonyManager manager = (TelephonyManager) getApplicationContext().getSystemService(getApplicationContext().TELEPHONY_SERVICE);
  
          manager.listen(new PhoneStateListener() {
              @Override
              public void onCallStateChanged(int state, String phoneNumber) {
  //                super.onCallStateChanged(state, phoneNumber){
                  if (TelephonyManager.CALL_STATE_IDLE == state
                          &amp;&amp; recorderstarted
                  ) {
                      Log.i(&quot;Hello&quot;, &quot;onCallStateChanged: Before stoptel&quot; + (TelephonyManager.CALL_STATE_IDLE == state) );
                      Log.i(&quot;Hello&quot;, &quot;onCallStateChanged: Before stoprec&quot; + (recorderstarted) );
                      rec.stop();
                      rec.reset();
                      rec.release();
                      recorderstarted = false;
                      stopSelf();
                      Log.i(&quot;Hello&quot;, &quot;onCallStateChanged: After stoptel&quot; + (TelephonyManager.CALL_STATE_IDLE == state) );
                      Log.i(&quot;Hello&quot;, &quot;onCallStateChanged: After stoprec&quot; + (recorderstarted) );
                  } else if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
                      try {
                          Log.i(&quot;Hello&quot;, &quot;onCallStateChanged: Before start&quot; + (TelephonyManager.CALL_STATE_OFFHOOK == state));
                          rec.prepare();
  //                        Thread.sleep(2000);
  
                          rec.start();
                          recorderstarted = true;
                          Log.i(&quot;Hello&quot;, &quot;onCallStateChanged: After start&quot; + (TelephonyManager.CALL_STATE_OFFHOOK == state));
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
          }, PhoneStateListener.LISTEN_CALL_STATE);
  
          return START_STICKY;
  //        return super.onStartCommand(intent, flags, startId);
      }
  }
  </code></pre>
  <p>I made this app from <a href="https://www.youtube.com/watch?v=NWyvWhG9uJQ" rel="nofollow noreferrer">this video</a>, Thanks for taking time to answer my question, if you need code from other class please comment under my question,
  I added the runtime permission code and I added the permission code to my <code>manifest</code> but my problem was not solved, please help me, Thanks (-:</p>