14

What must I write to send a recorded audio file as the microphone input in android programmatically?

Example:

User records "hello world".

He can then play the recording in a call.

theAnonymous
  • 1,701
  • 2
  • 28
  • 62
  • Can you clarify a bit? You want to record audio then replay it during a call? As in, you want to have the user make a call, maybe talk for a bit, then be able to push a button and replay their previously recorded message? Or do you want the user to record audio, then make that the entire call itself? I have done the latter in a project myself. – RayfenWindspear Mar 09 '17 at 18:52
  • Hi, thanks for your reply. Either way will do. I just need to know how to play an audio file and make it stream into the microphone so that the guy on the other side hears the audio. Thanks. – theAnonymous Mar 09 '17 at 19:45
  • Ah, my app is doing something different. We are playing back via Twilio API. According to a number of sources, streaming audio to a call isn't possible without root. Apparently it's a security thing in Android itself. See possible duplicate http://stackoverflow.com/questions/5268964/stream-audio-to-a-phone-call-android – RayfenWindspear Mar 09 '17 at 19:52
  • Is it possible to just stream the audio from a file into the microphone, but not in a call? – theAnonymous Mar 09 '17 at 22:34
  • Well you can Send a Share Link , Redirecting the to the Audio source. – Nandhu Mar 15 '17 at 18:21
  • not clear what you want to do, did you tried anything? Post code then show screenshots of anything which help us to understand what you are trying to achieve. – Maveňツ Mar 16 '17 at 05:52

2 Answers2

10

once you have a recorded file, you can open it as a InputStream, or any other way. BUT if you specifically looking for something like injecting the audio into a running call, then this is not possible. it's protected OS-level.

unless you are dealing with custom ROMs and modified kernels. which is not official

Yazan
  • 6,074
  • 1
  • 19
  • 33
  • 1
    Yep, it is not possible! – Gordon Freeman Mar 16 '17 at 11:26
  • 1
    But this would mean that "Answering Machine" type apps are completely dead in the water – Someone Somewhere Jul 12 '17 at 12:44
  • Oh, I would have thought it would be possible, because of the existence of voice changer software. – Boris Mar 12 '19 at 16:57
  • 1
    What about calls like whatsapp or line or facebook? – Ari Seyhun Jan 02 '20 at 12:39
  • @Acidic9 my guess would be No as well, because this call is actually a specific feature to that app, a Voip or some network call implemented in that specific app in a specific way, altering this behavior will need changing the app source code or somehow manage to inject your data within that network connection, which is harder than the original question – Yazan Jan 02 '20 at 14:24
1

This might send you in the right direction.

AudioManager.OnAudioFocusChangeListener by using this you can get the state of the audiomanager in the below code "focusChange=AUDIOFOCUS_LOSS_TRANSIENT"(this state calls when music is playing in background any incoming call came) this state is completely in developers hand whether to play or pause the music. As according to your requriment as for question you asked if you want to play the music when call is in OFFHOOK STATE dont pause playing music in OFFHOOK STATE .And this is only possible when headset is disabled

AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);

OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
    public void onAudioFocusChange(int focusChange) {
        if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT
            // Pause playback (during incoming call) 
        } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
            // Resume playback (incoming call ends)
        } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
            am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
            am.abandonAudioFocus(afChangeListener);
            // Stop playback (when any other app playing music  in that situation current app stop the audio)
        }
    }
};

The concept of using an audio driver (this is for windows but it might work for android)

Just as there are printer drivers that do not connect to a printer at all but rather write to a PDF file, analogously there are virtual audio drivers available that do not connect to a physical microphone at all but can pipe input from other sources such as files or other programs.

I hope I'm not breaking any rules by recommending free software, but VB-Audio Virtual Cable should let you create a pair of virtual input and output audio devices. Then you could play an MP3 into the virtual output device and then set the virtual input device as your "microphone". In theory I think that should work.

If all else fails, you could always roll your own virtual audio driver. Microsoft provides some sample code but unfortunately it is not applicable to the older Windows XP audio model. There is probably sample code available for XP too.

Pieter de Vries
  • 825
  • 7
  • 18