Search code examples
androidaudioandroid-audiomanagerringtone

Control Soundpool volume when receiving a call


I got an Android application which uses Linphone for VOIP calls.

When I receive a call, I'm starting a ringtone of my own (a raw mp3 file) like this:

final AudioManager audioManager = (AudioManager)ctx.getSystemService(Context.AUDIO_SERVICE);
final float maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, (int)maxVolume , 0);
soundPool.play(id, maxVolume , maxVolume , 1, 0, 1.0f);

The sound I want is playing but with a low volume. The call itself is accepted automatically, without the need of permission from the receiving end.

If I receive the call and end the call before the sound finished, the sound will continue (this is ok) but the volume will suddenly be at a maximum like I wanted in the first place. This means, the device might be entering a call state in which the volume of the soundpool is automatically being decreased, and when the call ends, the sound can have the maximum value.

How can I set the volume to maximum even during a call?


Solution

  • Since the volume is being automatically reduced, I had to try and accepting the call only after playing the sound. Since SoundPool does not support event handling when a sound finishes playing, I have switched to using a MediaPlayer which does support it, so I'm playing the ringing sound and then accepting the call after that.