3

Anyone know how can I increase/decrease the volume of the sound from mediaplayer by clicking on the volume button at the side of the phone? And how can I mute the sound if the phone was set to silent mode?

Code I'm using:

mp = MediaPlayer.create(this, R.raw.sound);
mp.start();
reson90
  • 137
  • 2
  • 5
  • 11

2 Answers2

7

I think this is what you might be interested in:

1, increase/decrease the volume of the sound: Link Here

Basically Override the onKeyDown and then call mp.setVolumn(float leftVolume,float rightVolume);

2, mute the sound if the phone was set to silent mode

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

switch (am.getRingerMode()) {
    case AudioManager.RINGER_MODE_SILENT:
        mp.setVolume(0,0);
        break;
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
Fei
  • 1,906
  • 20
  • 36
1

Another way to do it is in your mediaPlayer to set

mp.setAudioStreamType(AudioManager.STREAM_RING);
jcage
  • 651
  • 6
  • 14