22

I've rooted Nexus phone with lollipop. I'm trying to record incoming & outgoing calls.

My application extends DeviceAdminReceiver with policy of <force-lock />

I'm able to create the file with size > 0bytes. But when I play it, it is blank.

Please take a look at the code below:

Manifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.STORAGE" />
.....
<service android:name=".RCService"
  android:exported="false" >
</service>
.....
 <receiver
            android:name=".utils.MyDeviceAdminReceiver"
            android:permission="android.permission.BIND_DEVICE_ADMIN" >
            <intent-filter>

                <!-- This action is required -->
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
                <action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
                <action android:name="android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED" />
            </intent-filter>

            <!-- This is required this receiver to become device admin component. -->
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/device_admin" />
        </receiver>


        <receiver android:name=".CallRecoderBroadcast">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>

device_admin.xml

<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
    <uses-policies>
        <force-lock />
    </uses-policies>
</device-admin>

CallRecoderBroadcast.java

/**
 * Lifecycle :
 * Incoming call : RINGING -> OFFHOOK -> IDLE
 * Outgoing Call : OFFHOOK -> IDLE
 */
public class CallRecoderBroadcast extends BroadcastReceiver {

    private static final String TAG = "CallRecoderBroadcast";
    private static final String ACTION_PHONE = "android.intent.action.PHONE_STATE";
    private static final String OUTGOING_CALL = "android.intent.action.NEW_OUTGOING_CALL";

    private static Boolean mCallOnGoing = Boolean.FALSE;
    private MediaRecorder mRecorder = new MediaRecorder();
//    private File mFile = null;
    private String mNumber;

    @Override
    public void onReceive(Context context, Intent intent) {

        try {

            if (intent.getAction().equals(ACTION_PHONE)) {

                Bundle bundle = intent.getExtras();
                String state = bundle.getString(TelephonyManager.EXTRA_STATE);

                Log.e(TAG, state);

                if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

                    Log.e(TAG, bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER));
                    mNumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);


                } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

                    if (!mCallOnGoing) {
                        recordCall(context);
                    }


                } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {

                    if (mCallOnGoing) {
                        stopRecording();
                    }

                }

            } else if (intent.getAction().equals(OUTGOING_CALL)) {
                Log.e(TAG, "OUTGOING cALL");
                Log.e(TAG, intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER));
                mNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);


            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

    }

    private void recordCall(Context context) throws IOException, CustomExcpetion {

        File file = prepareFile(context);

        if (file.exists()) {
            mCallOnGoing = Boolean.TRUE;
            mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mRecorder.setOutputFile(file.getAbsolutePath());
            mRecorder.prepare();
            mRecorder.start();
        }

    }

    private void stopRecording() {
        mCallOnGoing = Boolean.FALSE;
//         mRecorder.stop();
        mRecorder.release();
    }

    private File prepareFile(Context context) throws CustomExcpetion {
        File outputFile = null;
        try {
            File dir = new File(context.getFilesDir(), "recorderdFiles");
            if (!dir.exists()) {
                dir.mkdirs();
            }

            Long timeStamp = System.currentTimeMillis();

            outputFile = new File(dir, timeStamp.toString());
            if (!outputFile.exists()) {
                outputFile.createNewFile();
            }
        } catch (IOException exp) {
            throw new CustomExcpetion(context, exp);
        }
        return outputFile;
    }
}

P.S. - I've also tried with example given here, but it only working form Mic & not for uplink & downlink at the same time.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
adam black
  • 607
  • 5
  • 14
  • [Check this link](http://stackoverflow.com/questions/24139332/voice-call-recording-in-android-using-mediarecorder) Check this link –  Nov 05 '15 at 10:49
  • Why was it needed to mention the app has admin rights? Does it help in any way? Anyway, did you find a way to handle most devices? For some reason, this app does a very good job even with devices that seem impossible to record from: https://play.google.com/store/apps/details?id=com.boldbeast.recorder&hl=en – android developer Jun 21 '18 at 07:08

1 Answers1

1

Unfortunately, voice call recorder is not supported on that device, I think that will work fine on other devices.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
S. Alawadi
  • 144
  • 1
  • 6