5

In my app, I am trying to set the volume when playing an audio clip to the maximum level but it doesn't appear to have any affect. I have to manually adjust the volume to the maximum level. Here's my code:

MediaPlayer mp = new MediaPlayer();
mp.setVolume(1, 1);
Johann
  • 27,536
  • 39
  • 165
  • 279

3 Answers3

10

AudioManager

int origionalVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
4

Use This

it s for total volume

AudioManager mgr = (AudioManager) appContext.getSystemService(Context.AUDIO_SERVICE);
        int valuess = 9;//range(0-15)
mgr.setStreamVolume(AudioManager.STREAM_MUSIC, valuess, 0);

it is for left right while current song is playing...

AudioTrack m = (AudioTrack) appContext.getSystemService(Context.AUDIO_SERVICE);
m.setStereoVolume(leftVolume, rightVolume);

it works for me.

Poovizhirajan N
  • 1,263
  • 1
  • 13
  • 29
1

Using AudioManager you can control the volume of media player.

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

also from MediaPlayer

public void  setVolume  (float leftVolume, float rightVolume)

for example you can use this method as,

int maxVolume = 100;
float log1=(float)(Math.log(maxVolume-currVolume)/Math.log(maxVolume));
mp.setVolume(1-log1);
No_Rulz
  • 2,679
  • 1
  • 20
  • 33