Stream audio to a phone call Android

I am attempting to send an audio stream out over a phone call on Android.

For example, to create an app which would play some custom on-hold music, or answer a call and play a recording/audio file. I know it is possible to have an app automatically answer a call, but can it send audio to the caller?

If it is possible, please let me know what classes/functions handle this.


Solution 1:

Writing to the phone call stream IS possible, but not from the app level on a stock (non rooted) phone.

When a phone call is initiated the mic is "typically" (really depends on the specific phone) routed directly to the baseband, ie skipping the main processor altogether.

For outgoing audio: mic->codec->baseband For incoming audio: baseband->codec->speaker

If it were always routed: mic->codec->mainprocessor->codec->baseband

Then the stream would be "presumably" available if the Android APIs (frameworks) supported accessing it.

The reason I say it is possible is because the audio (for nearly all smartphones now) is connected via SlimBus This allows dynamic changing of audio paths. It is however done in the kernel via the codec driver living in ALSA.

So.... were you so motivated, you could get the source for the Linux kernel for a phone and modify the codec/ALSA driver to allow you to change what happens when the call audio path is setup.

Of course then you would incur latency with the new path, breaking the call/latency standards AT&T setup (that Audience helped them write...) and the baseband chip might reject your audio as it's not timely.

Lastly you would need to modify the Android source (frameworks) to grow the API to support injecting audio onto that stream. (You'd need to make big mods to mediaserver, audioflinger in particular...)

It's complicated, but there is your answer. Cheers, :-)

Solution 2:

This must be possible because phones will allow Bluetooth to send audio as a microphone.

Since I need to solve this problem one idea I have is to essentially send a bluetooth signal to myself "cloaked" as a microphone input.

Another idea (apologies if it sucks) is to reverse the speaker as a microphone. Since it seems the speaker has access to the hardware required, there may be a way to inject the audio data stream that way.

Since I'm entirely new to coding on Androids with the SDK please excuse the initial botchery of this post, I'll update as I/we figure this out.

Solution 3:

I know the apps that are already sending all system audio to phone's earpiece even the songs you plays in your media player without root or any other special permission.

After some research I found that it can be done using Audio Manager class. For example

 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
 audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
 audioManager.setSpeakerphoneOn(false);

Why not to set default output speakers as device earpiece and then play audio using MediaPlayer class?