Reputation: 1804
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.
Upvotes: 14
Views: 10434
Reputation: 825
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.
Upvotes: 1
Reputation: 6082
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
Upvotes: 10