0

I am developing an app. I need to play the music when i am in a call. But i am facing with the problem that whenever i try to play the music in a call it is not that much loud as it is when there is no call. Is there any way to control the background music when in a call. My music volume becomes softer when i am in a call but it becomes louder as soon as i end the call.

final AudioManager mAudioManager = (AudioManager) ctx
                    .getSystemService(AUDIO_SERVICE);
            final int originalVolume = mAudioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC);
            mAudioManager
                    .setStreamVolume(
                            AudioManager.STREAM_MUSIC,
                            mAudioManager
                                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                            0);
            mp = new MediaPlayer();


            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

            mAudioManager.setMode(AudioManager.MODE_NORMAL);

            Class audioSystemClass = Class
                    .forName("android.media.AudioSystem");
            Method setForceUse = audioSystemClass.getMethod(
                    "setForceUse", int.class, int.class);
            // First 1 == FOR_MEDIA, second 1 == FORCE_SPEAKER. To go
            // back to the default
            // behavior, use FORCE_NONE (0).
            setForceUse.invoke(null, 1, 1);

this is my code.

Michael
  • 57,169
  • 9
  • 80
  • 125
Developer
  • 385
  • 1
  • 3
  • 18

2 Answers2

1

I see that you're trying to use the "force music streams to the loudspeaker"-method I posted in an answer to another question.

The problem is that during voice calls, music streams will follow the voice call's routing and ignore the FOR_MEDIA force flag. So what you perceive as lower volume is most likely the result of the music being played from the earpiece instead of from the loudspeaker.

To my knowledge, there's no way of routing the music to the loudspeaker during an ongoing call without also routing the voice call to the loudspeaker (i.e. using setSpeakerPhoneOn).

Community
  • 1
  • 1
Michael
  • 57,169
  • 9
  • 80
  • 125
0

Depending on how your phone manufacturer/carrier implemented Android AudioPolicyService, some implementation lowers STREAM_MUSIC volume when MODE_IN_CALL/MODE_IN_COMMUNICATION is in session.

I have not tried this, but a likely workable solution is to use STREAM_VOICE_CALL instead of STREAM_MUSIC. Your audio stream should then play along with the in call audio stream just fine.

Kevin I. Wu
  • 226
  • 1
  • 4