1

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?

I have considered this post here How to record phone calls in android? and adapted the code he posted @Mukesh Parmar

@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();
        }
    };
}

0 Answers0