55

Can I change the media volume? and how? I used this so far:

setVolumeControlStream(AudioManager.STREAM_MUSIC);

But have a seekbar and want to change the media volume, not ring volume.

So can someone show me how to just change the media volume at onCreate() and I fix the seekbar later.

Cristian
  • 198,401
  • 62
  • 356
  • 264
carefacerz
  • 1,287
  • 3
  • 16
  • 27
  • I use this method too. For some reason i am not able to call:- context.setVolumeControlStream(AudioManager.STREAM_MUSIC) from my surface view? i cant replace "context" with "this" since im not in an Activity.. any hints? – user3833732 Feb 21 '15 at 14:45
  • I want to silent only ringer on incoming call not playing video .How I can solve this ,I am having issue with xiaomi mobile... – Sagar Jan 24 '18 at 07:47

5 Answers5

92

The right method to use would be setStreamVolume on your AudioManager. It could looks like this

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

audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                             [int value],
                             [if desired a flag]);

An example use of the flag is to get the beep when setting the volume so the user can hear the outcome. The flag for that would be AudioManager.FLAG_PLAY_SOUND.

You could use AudioManager.FLAG_SHOW_UI if you don't want to play a sound but display a toast with the current value. The use has to get a feedback tho. Doesn't matter if it is audible or visual.

To get the maximal valid value for the given stream you just call getStreamMaxVolume() on the AudioManager and get an integer back which represents ... well the maximal valid value for the volume.

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
  • 1. i dont want any sound and i get a error if i remove the flag. – carefacerz Nov 15 '10 at 17:33
  • 2. What is the possible value where you typed [int value] ? sorry i accedentally pressed enter and thought i just jump one row instead of posting. – carefacerz Nov 15 '10 at 17:34
  • does this need any permission? – wutzebaer Jul 29 '13 at 17:50
  • Hey, I was trying to create a mute and un-mute button in my app. I want the unmute to set to the volume of the users volume before entering the app. I tried to use your explanation, but ran into an issue: http://stackoverflow.com/questions/33589116/the-audio-volume-is-zero?noredirect=1#comment54954950_33589116 – Ruchir Baronia Nov 08 '15 at 00:59
  • @wutzebaer No. But there is a permission called "MODIFY_AUDIO_SETTINGS" , which is a bit odd... – android developer Jun 07 '16 at 10:15
40
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;
        // Update based on @Rene comment below:
        return super.onKeyDown(keyCode, event);
    }
}
Anthony Graglia
  • 5,355
  • 5
  • 46
  • 75
  • 11
    Works, but the default should be: return super.onKeyDown(keyCode, event); Else you lose the back key to end the app e.g. – Rene Apr 12 '11 at 13:42
  • 2
    Very true... I probably caught that too but excluded it for the example and then kept false in there because of scrolling issues with the trackball. – Anthony Graglia Apr 13 '11 at 09:04
  • Note that the documentation for `adjustStreamVolume` and other methods says *"This method should only be used by applications that replace the platform-wide management of audio settings or the main telephony application."* https://developer.android.com/reference/android/media/AudioManager.html#adjustStreamVolume(int,%20int,%20int) – TalkLittle Aug 15 '17 at 17:04
  • @trgraglia: Is there any special API call required to set volume of external headset connected through my audio slot? – vgokul129 May 28 '18 at 14:01
11

You can use the following code to handle Volume using a SeekBar:

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

SeekBar sbVolumeBooster = (SeekBar) findViewById(R.id.sbVolumeBooster);
sbVolumeBooster.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)); 
sbVolumeBooster.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));  

sbVolumeBooster.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
{
    @Override
    public void onStopTrackingTouch(SeekBar arg0) 
    {
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) 
    {
    }

    @Override
    public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) 
    {
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                progress, 0);  // 0 can also be changed to AudioManager.FLAG_PLAY_SOUND
    }
});
Daniel
  • 2,355
  • 9
  • 23
  • 30
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
5

Giving a 0 - in the flags avoids getting a visual and audio indicator . That's good when you implement your own audio bar and indicator and you don't want android to add anything.

Yoni
  • 106
  • 1
  • 3
1

Use adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, flags);

http://developer.android.com/reference/android/media/AudioManager.html#adjustStreamVolume(int, int, int)

Computerish
  • 9,590
  • 7
  • 38
  • 49
  • Honestly I'm not sure, but this is the list of possible flags: http://developer.android.com/reference/android/media/AudioManager.html#FLAG_ALLOW_RINGER_MODES You could also try giving it a number like 0 that isn't used for any of the flags. This might indicate not using any flag. – Computerish Nov 14 '10 at 19:18
  • 0 would imply no flags. – Abandoned Cart May 03 '17 at 20:55