Maid786 Maid786 - 1 year ago 75
Android Question

Android: Incoming call auto answer, play a audio file

In Android, at the time of an incoming call, I want to receive it. Then, from my app, automatically play an audio file during a call and the other party should hear it. Is this possible?

Answer Source

What you are talking about is not exactly possible with android. Android has no access to the in-call audio stream.

Though i can give you a little bit idea about how to do it.

first to intercept incoming call, you need to register a broadcast receiver, which is invoked whenever call is received

public void onReceive(final Context context, Intent intent) 
    TelephonyManager telephonyManager = null;
    PhoneStateListener listener = new PhoneStateListener() 
        public void onCallStateChanged(int state, String incomingNumber) 
            switch (state) 
            case TelephonyManager.CALL_STATE_IDLE:
                Toast.makeText(context, "Call Ended..", Toast.LENGTH_LONG).show();
                Log.i("stop", "Call Ended....");
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Toast.makeText(context, "Call Picked..", Toast.LENGTH_LONG) .show();
                Log.i("received", "Call Picked....");
            case TelephonyManager.CALL_STATE_RINGING:
                Toast.makeText(context, "Call Ringing.." + incomingNumber,5000).show();
    // Register the listener with the telephony manager
    telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

Also change your manifest,

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
        android:label="@string/app_name" >
        <receiver android:name="MyReceiver">


                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />


With this, you can intercept the incoming call and pick the call, now you can try playing some mp3 file in the

case TelephonyManager.CALL_STATE_OFFHOOK:
                // Play mp3 file here

Hope it helps. Must try this and tell me the experience.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download