1
votes

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);
}
2

2 Answers

2
votes

A few things could be happening here. First, check your target device doesn't have a fixed volume policy. As outlined in AudioManager.setStreamVolume()

This method has no effect if the device implements a fixed volume policy as indicated by isVolumeFixed().

Next, make sure you are not setting the volume above the maximum setting. you can use AudioManager.getStreamMaxVolume() to find that number.

If neither of these are the issue, you'll have to post some code as to how you're using the API.