Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to record call using MediaRecorder

Tags:

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.

like image 629
adam black Avatar asked Oct 24 '15 20:10

adam black


People also ask

How do I record a phone call in process?

On your Android device, open the Voice app and tap the menu, then settings. Under calls, turn on incoming call options. When you want to record a call using Google Voice, simply answer the call to your Google Voice number and tap 4 to start recording.


1 Answers

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

like image 70
S. Alawadi Avatar answered Nov 14 '22 13:11

S. Alawadi