How to programmatically adjust the notification sound level?

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 it, the volume of the notification sound should be regulated by STREAM_NOTIFICATION. But the notification always plays with the same volume, despite the fact that the volume number is in the setStreamVolume method. Why is this?

+5
source share
3 answers

I went the other way. This is not quite the answer to my question, but appropriate. When I play the notification in STREAM_MUSIC, everything is fine. Thus, the notification is reproduced exactly with the volume that I pass as a 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();
}
+5
source

, , , , . AudioManager.FLAG_PLAY_SOUND r.play() . , , . . , 0.

, , 0. 1,

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

, setStreamMute, 0.

+2

, ,

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

ringtone(), .

-1

All Articles