53

Is there a way to get/set media volume? I have tried the following:

AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);

but it returns the ringtone volume.

gonzobrains
  • 7,856
  • 14
  • 81
  • 132
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196

8 Answers8

63
private AudioManager audio;

Inside onCreate:

audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

Override onKeyDown:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
        return true;
    default:
        return false;
    }
}
Anthony Graglia
  • 5,355
  • 5
  • 46
  • 75
  • 1
    you have made my day, solution worked very well, exactly what I needed. Just to let everyone know, this shows the popup with Media volume progress bar and allows you to easily select volume in your application. Thank you. – Alin Mar 27 '11 at 12:54
  • No problem... You of course can also handle other things with the switch statement too like menu and back button key downs. – Anthony Graglia Mar 28 '11 at 07:03
  • 4
    It's too hard. And produces unwanted sound like user changed ring tone volume. The best solutions is: http://stackoverflow.com/a/10919403/148387 – Vadim Jun 07 '12 at 22:11
  • 1
    Just what I was after. Thank you @trgraglia! – miek Jun 19 '12 at 06:49
  • 1
    The documentation specifically says not to do this: http://developer.android.com/training/managing-audio/volume-playback.html "You may be tempted to try and listen for volume key presses and modify the volume of your audio stream that way. Resist the urge." – Wayne Uroda Oct 10 '13 at 04:09
  • It also states: "The type of the audio stream whose volume should be changed by the hardware volume controls. It is not guaranteed that the hardware volume controls will always change this stream's volume (for example, if a call is in progress, its stream's volume may be changed instead). To reset back to the default, use USE_DEFAULT_STREAM_TYPE." The documentation seams to have changed focus but it is also nice to have a finite solution which leaves less error: http://developer.android.com/reference/android/app/Activity.html#setVolumeControlStream(int) . It is good to know though. – Anthony Graglia Oct 10 '13 at 06:24
  • 2
    You should not just return `false` as default since this will effectively disable other buttons such as the back button. Instead, `super.onKeyDown(keyCode, event);` – Torsten Römer Aug 06 '15 at 12:10
  • Can you help me on this question about volume? http://stackoverflow.com/questions/35256364/volume-always-the-same – Ruchir Baronia Feb 07 '16 at 17:10
55

Instead of AudioManager.STREAM_RING you shoul use AudioManager.STREAM_MUSIC This question has already discussed here.

Community
  • 1
  • 1
Sandy
  • 6,285
  • 15
  • 65
  • 93
34
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float percent = 0.7f;
int seventyVolume = (int) (maxVolume*percent);
audio.setStreamVolume(AudioManager.STREAM_MUSIC, seventyVolume, 0);
vrunoa
  • 766
  • 8
  • 15
33

Have a try with this:

setVolumeControlStream(AudioManager.STREAM_MUSIC);

hanoo
  • 4,175
  • 2
  • 26
  • 19
  • 5
    Quote: "You may be tempted to try and listen for volume key presses and modify the volume of your audio stream that way. Resist the urge. Android provides the handy setVolumeControlStream() method to direct volume key presses to the audio stream you specify." http://developer.android.com/training/managing-audio/volume-playback.html#HardwareVolumeKeys – EricRobertBrewer Jun 02 '13 at 02:42
  • 2
    This is much better than trying to manage the volume yourself. – strangeluck Sep 05 '13 at 19:46
  • This should definitely be the answer. It also works with Activities that inherit NativeActivity. – Brent Apr 29 '14 at 00:21
19

You can set your activity to use a specific volume. In your activity, use one of the following:

this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
this.setVolumeControlStream(AudioManager.STREAM_RING);  
this.setVolumeControlStream(AudioManager.STREAM_ALARM);  
this.setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);  
this.setVolumeControlStream(AudioManager.STREAM_SYSTEM);  
this.setVolumeControlStream(AudioManager.STREAM_VOICECALL); 
Asaf Pinhassi
  • 15,177
  • 12
  • 106
  • 130
8

If you happen to have a volume bar that you want to adjust –similar to what you see on iPhone's iPod app– here's how.

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
        //Raise the Volume Bar on the Screen
        volumeControl.setProgress( audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
                + AudioManager.ADJUST_RAISE);
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        //Adjust the Volume
        audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
        //Lower the VOlume Bar on the Screen
        volumeControl.setProgress(audioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC)
                + AudioManager.ADJUST_LOWER);
        return true;
    default:
        return false;
    }
Chris M
  • 316
  • 3
  • 9
5

The following code will set the media stream volume to max:

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
    audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
    AudioManager.FLAG_SHOW_UI);
sskates
  • 1,478
  • 1
  • 13
  • 12
3

To set volume to 0

AudioManager audioManager;
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);

To set volume to full

AudioManager audioManager;
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);

the volume can be adjusted by changing the index value between 0 and 20

Rob
  • 26,989
  • 16
  • 82
  • 98
Eby Cloudins
  • 111
  • 8
  • 1
    The max volume isn't alway `20`. Maybe you can pass a bigger than max value without causing an issue, I don't know. But to get the max use `audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)`. – Mudlabs Sep 02 '19 at 06:53