Max Liu
Max Liu

Reputation: 11

mediaPlayer.setVolume didn't work when a phone in a silence

I customised an alarm clock, but it didn't work when the phone was in silent mode.

mediaPlayer = new MediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.example);
mediaPlayer.setVolume(1.0f, 1.0f);
mediaPlayer.start();

What could be the problem?

Upvotes: 0

Views: 756

Answers (1)

LaurentY
LaurentY

Reputation: 7653

Volume of MediaPlayer depends on volume of AudioManager for STREAM_MUSIC.

For instance, set volume to max for stream music, before play your sound, and maybe after re-set value to old value (0):

This solution not respect choice of user. He doesn't to be disturb if he put device in silent mode !

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);

Upvotes: 1

Related Questions