Android : How to set MediaPlayer volume programmatically?

66,186

Solution 1

Using AudioManager, you can simply control the volume of media players.

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

also from MediaPlayer (But I didn't try that)

setVolume(float leftVolume, float rightVolume)

Since: API Level 1

Sets the volume on this player. This API is recommended for balancing the output of audio streams within an application. Unless you are writing an application to control user settings, this API should be used in preference to setStreamVolume(int, int, int) which sets the volume of ALL streams of a particular type. Note that the passed volume values are raw scalars. UI controls should be scaled logarithmically.

Parameters

leftVolume left volume scalar

rightVolume right volume scalar

Solution 2

Hope this help

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

For Volume UP

 audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                        AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);

for Volume Down

audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                        AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);

Solution 3

You do have the setVolume method in the MediaPlayer class. See here

Solution 4

To Hide Volume Controll UI:

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

For Volume UP

audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_RAISE, 0);

for Volume Down

audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_LOWER, 0);

Solution 5

Read this page right here. It explains very well. Basically, unless your app is a replacement alarm clock, you need to make the following call in the "onCreate()" function:

setVolumeControlStream(AudioManager.STREAM_MUSIC);

In this way you can create the volume of your app using the hardware buttons.

Share:
66,186

Related videos on Youtube

Pattabi Raman
Author by

Pattabi Raman

Updated on July 09, 2022

Comments

  • Pattabi Raman
    Pattabi Raman almost 2 years

    How to set the mediaplayer volume programmatically. I use it for alarm notification. Any help is highly appreciated and thanks in advance.

  • Pattabi Raman
    Pattabi Raman over 12 years
    what is the minimum volume to set with value?
  • Pattabi Raman
    Pattabi Raman over 12 years
    if 0, it takes no volume. If 1, it is ringing with full volume on the device.
  • pioneerBhawna
    pioneerBhawna over 9 years
    hi dwivedi ji, can we adjust notification sound through our device's volume button ?
  • Azurespot
    Azurespot about 9 years
    What is the 20 number? I thought you said volume was from 0 to 1?
  • Azurespot
    Azurespot about 9 years
    Is there a way to raise the volume of the actual MediaPlayer file, not the phone volume? I am getting a MediaPlayer whose playback is very low, even if the phone volume is normal.
  • P Kuijpers
    P Kuijpers over 6 years
    20 is the max volume number (0 is minimum). But I've found out this can be different per device. You can get the max volume number using audioManager.getStreamMaxVolume(streamType) and do some nice math if you'd like adjust it in a more controlled way ;-)
  • xeruf
    xeruf over 6 years
    I personally prefer setVolume as it provides far more fine-grained control and is not dependent on the Output device (for example a bluetooth headset might have even less volume steps)
  • Abdullah Gheith
    Abdullah Gheith almost 5 years
    Note that setVolumes value range (0.0-1.0) is relative to the current media volume. So if the media volume is 0, then it won't matter what the value is, no sound is coming out :)
  • Vladyslav Berdnikov
    Vladyslav Berdnikov over 2 years
    twintyVolume is nice)