Auto Call Recorder App Source Code Android Studio

Here is a sample code for an auto call recorder app in Android Studio:

Auto Call Recorder
import android.content.Context;
import android.media.MediaRecorder;
import android.os.Environment;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

import java.io.File;
import java.io.IOException;

public class CallRecorder {
    private MediaRecorder recorder;
    private String phoneNumber;
    private boolean isRecording;

    private CallRecorder() {
        recorder = new MediaRecorder();
    }

    private static CallRecorder instance;

    public static CallRecorder getInstance() {
        if (instance == null) {
            instance = new CallRecorder();
        }
        return instance;
    }

    public void startRecording(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        PhoneStateListener callStateListener = new PhoneStateListener() {
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    phoneNumber = incomingNumber;
                }
                if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    // start recording
                    recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
                    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
                    File path = Environment.getExternalStorageDirectory();
                    try {
                        File dir = new File(path, "CallRecorder");
                        if (!dir.exists()) {
                            dir.mkdirs();
                        }
                        String fileName = "record_" + phoneNumber + ".mp4";
                        File recording = new File(dir, fileName);
                        recorder.setOutputFile(recording.getAbsolutePath());
                        recorder.prepare();
                        recorder.start();
                        isRecording = true;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (state == TelephonyManager.CALL_STATE_IDLE) {
                    // stop recording
                    if (isRecording) {
                        recorder.stop();
                        recorder.reset();
                        isRecording = false;
                    }
                }
            }
        };
        telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }
}

This code uses the MediaRecorder class to record audio from a phone call. It starts recording when the phone goes off the hook (when the call is answered) and stops recording when the call ends. It saves the recording as an MP4 file in a folder called “CallRecorder” on the device’s external storage. The file is named “record_[phone number].mp4”.

You will need to add the following permission to your app’s manifest file in order to use the MediaRecorder class:

<uses-permission android:name="android.permission.RECORD_AUDIO" />

Here are a few more things you might want to consider adding to your call recorder app:

  • A user interface for the app. You could create an activity with buttons to start and stop recording and display a list of recorded calls.
  • A way to play back recorded calls. You could use the MediaPlayer class to play back the recorded MP4 files.
  • A way to delete recorded calls. You could add a “Delete” button to the user interface, and use the File class to delete the corresponding MP4 file when the button is pressed.
  • A way to handle recording errors. You should catch any exceptions that might be thrown when preparing or starting the MediaRecorder, and display an error message to the user if necessary.
  • A way to handle runtime permissions. On Android 6.0 and above, you will need to request the RECORD_AUDIO and READ_EXTERNAL_STORAGE permissions at runtime. You can use the ContextCompat.checkSelfPermission() method to check if you have the necessary permissions, and the ActivityCompat.requestPermissions() method to request them if you don’t.

I am a Digital Marketer and Serial Entrepreneur. I am having years of experience in the field of digital marketing. I Love Hindi Blogging, Create Brands, and Run Paid Ads. I have helped more than 100+ Clients and Companies with their Online Reputations.

Leave a Comment