Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Media volume in Android?

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.

like image 698
carefacerz Avatar asked Nov 14 '10 18:11

carefacerz


People also ask

What is Media Volume on an Android phone?

The in-call volume refers to the volume of voice and video calls, while the media volume refers to the volume at which background music, videos, and audio effects are played.


5 Answers

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.

like image 62
Octavian A. Damiean Avatar answered Oct 02 '22 16:10

Octavian A. Damiean


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);
    }
}
like image 45
Anthony Graglia Avatar answered Oct 02 '22 14:10

Anthony Graglia


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
    }
});
like image 27
Zar E Ahmer Avatar answered Oct 02 '22 16:10

Zar E Ahmer


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.

like image 29
Yoni Avatar answered Oct 02 '22 15:10

Yoni


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

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

like image 25
Computerish Avatar answered Oct 02 '22 16:10

Computerish