I use MediaPlayer setVolume function to control the audio track volume, that is ok. but when I set Volume 0, then press the volume button, the volume is not 0. Why? please help me.
Asked
Active
Viewed 318 times
1 Answers
0
The volume you are changing is the volume of that mediaplayer instance not the device. To change device volume you need to create the AudioManager Object and use it to change device volume. Example :
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
Button upButton = (Button) findViewById(R.id.upButton);
upButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//To increase media player volume
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
}
});
Button downButton = (Button) findViewById(R.id.downButton);
downButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//To decrease media player volume
audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
}
});
You can look at this complete answer to override volume button from your device.

Sahdeep Singh
- 1,342
- 1
- 13
- 34