• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Record the audio of a phone call (android java development)

con7a04

Lurker
Jan 14, 2021
1
0
I am developing an app that records call audio. My app is able to record system audio but hearing an audio that the app has recorded I noticed that when I accept the call, the audio seems to stop and only seems to restart when the call is ended. Could somebody tell me why? I am using MediaRecorder. I leave you my code below.

my code:

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, "Registration started", 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("REDORDING :: ", e.getMessage());
                        e.printStackTrace();
                    } catch (IOException e) {
                        Log.e("REDORDING :: ", 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, "Record saved", 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() + "/" + 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,
                    "Error: " + what + ", " + 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,
                    "Warning: " + what + ", " + extra, Toast.LENGTH_SHORT)
                    .show();
        }
    };
}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones