Search code examples
androidandroid-mediaplayerandroid-audiomanager

Play sound via phone speaker during handsfree phone call


How to play sound in phone speaker during phone call (in handsfree)?

I try this:

audio.setMode(AudioManager.MODE_IN_CALL);
audio.setSpeakerphoneOn(true);

audio.setStreamVolume(AudioManager.MODE_IN_CALL, 20, 0);
mediaplayer.setStreamType(AudioManager.STREAM_VOICE_CALL);

But in this way - starts playing loud sound from the phone speaker, but the call is terminated in handfree and call is also playing in the phone speaker.

How is it possible to play a sound without affecting the call in handsfree? Thanks.


Solution

  • Use another stream to not interrupt the call audio:

    mediaplayer.setStreamType(AudioManager.STREAM_MUSIC);
    

    If you're just trying to send simple tones over the call itself then maybe:

    mediaplayer.setStreamType(AudioManager.STREAM_DTMF);