Easy way to play an audio file on call in android using twilio

Play an audio file on call is not a simple task. The simple fact is the microphone / input audio stream during a phone call is not able to be pre-processed or replaced with another stream of any form. So simply while one would think its easy to use media during a call, it simply is not possible due to the way the android system processes the audio. Play audio file android call is a limitation, imposed for security reasons and restricted at the OS level.

Let’s analyze the security threat, first of all. If you were able to play custom audio files to the callee,a whole world of cons opens up: you could trick customer supports, you could pretend to be someone else, you could give unauthorized purchase confirmations, and so on. For this reason, neither Android nor iOS allows this functionality.

On Android, you won’t be able to do so in a programmatic way, simply because the current APIs won’t allow you to play audio file android call. It is stated in the official documentation as well, as pointed out here. If you dig into the source code, you can probably enable this feature by accessing the microphone output during a phone call, but that would require running your custom version of Android. A good starting point would be the AudioTrack source, available here.

Why did Android/Google/Hardware do this to us?

Well your guess is as good as mine, but one would think its to do with the way this can be exploited. Having a application to mask your voice, a rouge application that answers calls for you and so on. Simply I myself can see plenty of good reasons why to not allow it, but really wish it was allowed/possible because with any technology people will be screwed over with anyway.

Alternative methods for this?

Well this is the tricky question, simply there is no true alternative if your core requirement is to play audio file android call to the other party from your phone. Some people revert to using VoIP for redirecting and handling this or other logic methods..

Here we have alternative with the help of Twilio. Twilio allows software developers to programmatically make and receive phone calls and send and receive text messages using its web service APIs. Twilio’s services are accessed over HTTP and are billed based on usage.

We make a project in which we use twilio for voice. Here we transfer the call to twilio using this you can not talk but you can send any voice to your call receiver. First of all set up an account on twilio.

Easy way to play audio file android call :

Step 1 : Make a call for token. Whenever we call then twilio send a token.

 // Correlate desired properties of the Device (from ClientProfile) to properties of the Capability Token
  Uri.Builder b = Uri.parse(CallWidget.TOKEN_SERVICE_URL).buildUpon();
/*  if (newClientProfile.isAllowOutgoing()) {
      b.appendQueryParameter("allowOutgoing", newClientProfile.allowOutgoing ? "true" : "false");
  }
  if (newClientProfile.isAllowIncoming() && newClientProfile.getName() != null) {
      b.appendQueryParameter("client", newClientProfile.getName());
  }*/
  Ion.with(context)
          .load(b.toString())
          .asString()
          .setCallback(new FutureCallback<String>() {


              @Override
              public void onCompleted(Exception e, String capabilityToken) {
                  if (e == null) {
                      Log.d("Twillio", capabilityToken);

                      // Update the current Client Profile to represent current properties
                      // this.clientProfile = newClientProfile;

                      // Create a Device with the Capability Token
                      createDevice(capabilityToken,context);
                  } else {
                      Log.e("Twillio", "Error retrieving token: " + e.toString());
                      Toast.makeText(context, "Error retrieving token", Toast.LENGTH_SHORT).show();
                  }
              }
          });

Step 2 : After receive the token from twilio then make a call for audio voice. Using this audio url a voice will be generate when receiver receive the call.

Map<String, String> params = new HashMap<String, String>();
params.put("To", URLEncoder.encode(urls[0]));
params.put("From",URLEncoder.encode(TWILLIO_NUMBER));
params.put("Url","https://twilio-mobile.herokuapp.com/");
params.put("Method", "GET");
params.put("StatusCallbackMethod", "GET");
params.put("FallbackMethod","GET");
params.put("Record",String.valueOf(false));
httpRequestor.setHeader("AC03284dcdb6209d0a1f4ca1e464d2551e:3574fce07b60065e74c79de06ad4d56b");
result = httpRequestor.downloadData(AUDIO_SERVICE_URL, "POST", params);

When a call hit at the audio url then a voice will be generate that will be hear on the receiver side. You can make any type of voice even you can use any song also.

Conclusion

If you want to implement play audio file android call in your project then you will find these steps more easy. Implementation of play audio file android call has very easy steps as described above. You can edit the xml and activity files in your own way to make it more attractive as per your requirements.

Go to Paly an audio file on call Example on Github

Refrences

Twilio : Twilio document that gives the information about twilio.

Android : Android document that gives the information about android that is useful for developer.

Leave a Comment

Scroll to Top