I have an app that is producing a sound on Android, my code is setting volume on all audio streams to make sure all streams has volume set. Here are that streams:
STREAM_MUSIC
STREAM_RING
STREAM_ALARM
STREAM_NOTIFICATION
STREAM_VOICE_CALL
STREAM_DTMF
STREAM_SYSTEM
If I change the volume using:
audioMgr.setStreamVolume(audioStream, newVolume, 0);
I don't see the volume is being changed, it play the default volume but the touch sound volume is getting changed using above method. Here is that code:
HashMap<Integer, Integer> maxVolumeMap= new HashMap<Integer, Integer>();
AudioManager audioMgr = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
maxVolumeMap.put(AudioManager.STREAM_MUSIC,audioMgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
maxVolumeMap.put(AudioManager.STREAM_RING,audioMgr.getStreamMaxVolume(AudioManager.STREAM_RING));
maxVolumeMap.put(AudioManager.STREAM_ALARM,audioMgr.getStreamMaxVolume(AudioManager.STREAM_ALARM));
maxVolumeMap.put(AudioManager.STREAM_NOTIFICATION,audioMgr.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION));
maxVolumeMap.put(AudioManager.STREAM_VOICE_CALL,audioMgr.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL));
maxVolumeMap.put(AudioManager.STREAM_DTMF,audioMgr.getStreamMaxVolume(AudioManager.STREAM_DTMF));
maxVolumeMap.put(AudioManager.STREAM_SYSTEM,audioMgr.getStreamMaxVolume(AudioManager.STREAM_SYSTEM));
int newVolume = 7; //this is new volume value
Iterator<Integer> itr = maxVolumeMap.keySet().iterator();
while (itr.hasNext())
{
//set new volume for all audio streams
int audioStream = itr.next();
float deviceVolume = (float)(newVolume/10.0f) *maxVolumeMap.get(audioStream);
audioMgr.setStreamVolume(audioStream, Math.round(deviceVolume), 0);
}