2

I follow the example of Record phone calls on android phone? and put it on a BroadcastReceiver to try to record MIC voice (I know it is still limited to record the other side) when phone call incoming and outgoing. My question is: how can I get the state when the user pick up the phone. Because when it is ringing, it also will go to the action of "android.intent.action.PHONE_STATE".

My code:

public class PhoneCallReceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        String action = intent.getAction();
        if (action.equals("android.intent.action.PHONE_STATE") 
        {
            // Phone call recording
            try {
                recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
                recorder.setOutputFile(<my output dir>);
                recorder.prepare();
                recorder.start();
                recordStarted = true;
                telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
                telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
            } catch(Exception ex) {

            }
        } 
    }
}


private final PhoneStateListener phoneListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        try {
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING: {
                    // 
                    break;
                }
                case TelephonyManager.CALL_STATE_OFFHOOK: {
                    // 
                    break;
                }
                case TelephonyManager.CALL_STATE_IDLE: {
                    if (recordStarted) {
                        recorder.stop();
                        recordStarted = false;
                    }
                    break;
                }
                default: { }
            }
        } catch (Exception ex) {
        }
    } 
};

the code in AndroidManifest.xml

<receiver android:name=".PhoneCallReceiver" android:enabled="true">
    <intent-filter android:priority="1000">
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

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

Based on Android 2.1 SDK and tested in HTC EVO 4G (Android 2.2)

Community
  • 1
  • 1
Richard Ye
  • 191
  • 1
  • 5
  • 12

2 Answers2

1

If you want to record voice from both ends use (according to Android Docs)

recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);

I have not tried this but hope will work for you and to answer your question, have you looked function getCallState() of TelephonyManager?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
bGuruprasad.com
  • 362
  • 3
  • 10
0

You can use the android.intent.action.ANSWER action instead of using the android.intent.action.PHONE_STATE action. You are able to record the voice when the call is answered.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
  • Thank you. But I found it triggers TelephonyManager.CALL_STATE_OFFHOOK state when I pick up phone call and intent.getAction() never become android.intent.action.ANSWER but always android.intent.action.PHONE_STATE. Btw, I develop with 2.1 SDK and test it on Android 2.2 (HTC EVO 4G) – Richard Ye Oct 28 '11 at 15:43