5

I have this method in my main activity

private void beep()
{
    AudioManager manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    manager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0,
            AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
    Uri notification = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone r = RingtoneManager.getRingtone(getApplicationContext(),
            notification);
    r.play();
}

As I understand, notification sound volume should be regulated by STREAM_NOTIFICATION. But notification always plays with the same volume despite that volume number in setStreamVolume method. Why is that?

kaiz.net
  • 1,984
  • 3
  • 23
  • 31
Vitalii Korsakov
  • 45,737
  • 20
  • 72
  • 90

4 Answers4

9

I went another way. It's not exactly answer to my question but appropriate one. When I play notification in STREAM_MUSIC everything is fine. So notification plays exactly with volume I pass as parameter to the function

private void beep(int volume)
{
    AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    manager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);

    Uri notification = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    MediaPlayer player = MediaPlayer.create(getApplicationContext(), notification);
    player.start();
}
Vitalii Korsakov
  • 45,737
  • 20
  • 72
  • 90
  • The sound is still playing when notification is off from device settings. Is anybody facing this issue using the above code? Note - I'm using GCM latest library. – Ruby Verma Feb 23 '17 at 22:55
3

First of all, I hope you realize that you are attempting to play two notifications right after each other, so there might be a conflict about that. AudioManager.FLAG_PLAY_SOUND and r.play() will both try playing the sound. It is usually enough to give user one type of information, either by UI or by playing the beep with new volume. I suggest you delete one of the flags. If you don't need any, just give it 0.

Coming to the main question, I am not sure if you can set volume level to 0. Try putting 1, like

manager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 1, AudioManager.FLAG_SHOW_UI);

If you want to mute the notification, try using setStreamMute, which is equivalent to setting the volume to 0.

Erol
  • 6,478
  • 5
  • 41
  • 55
0

For my case eventually this seemed to be working. It would make your audio volume relative to the stream current volume but it was useful for my need.

MediaPlayer player = MediaPlayer.create(getApplicationContext(), notificationUri);
player.setVolume(toneLevel, toneLevel);

toneLevel is between 0.0 - 1.0 so you don't even need to find the range of your stream..

Rock_Artist
  • 555
  • 8
  • 14
-2

Write a function as follow, or copy and paste the following,

public void ringtone() {
    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
    } catch (Exception e) {}
}

and call ringtone() when you want to make a beep notification.

StarsSky
  • 6,721
  • 6
  • 38
  • 63
Ragu
  • 233
  • 2
  • 6
  • original question is about volume... he has almost exact snippet as this one so this has nothing to do with the volume. – Rock_Artist Dec 27 '18 at 15:40