Home > Android > How to tell android which volume (media/ringtone etc.) should be controlled by your app

How to tell android which volume (media/ringtone etc.) should be controlled by your app

February 1st, 2010 mat Leave a comment Go to comments

In my android apps I was getting annoying problems that whilst playing sound the volume buttons would control the media volume, but when no sound was playing they would control the ringtone volume. I found the following by trial and error, hopefully this post will help people with the same problem.

This is done by placing a call of setVolumeControlStream in the onCreate part of your activity which takes on of the following values

  • AudioManager.STREAM_MUSIC
  • AudioManager.STREAM_RING
  • AudioManager.STREAM_ALARM
  • AudioManager.STREAM_NOTIFICATION
  • AudioManager.STREAM_SYSTEM
  • AudioManager.STREAM_VOICECALL

Below shows the code required to set your applications default volume control to any of the above values:

Media Volume

this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

Ringtone Volume

this.setVolumeControlStream(AudioManager.STREAM_RING);

Alarm Volume

this.setVolumeControlStream(AudioManager.STREAM_ALARM);

Notification Volume

this.setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);

System Volume

this.setVolumeControlStream(AudioManager.STREAM_SYSTEM);

Voicecall Volume

this.setVolumeControlStream(AudioManager.STREAM_VOICECALL);

I imagine this is also possible via and XML settings, if anyone knows of this I’d be very grateful to know how!


Bookmark and Share
Categories: Android Tags: , ,
  1. scheich
    February 2nd, 2010 at 21:19 | #1

    thx

  2. OC
    February 4th, 2010 at 15:34 | #2

    Thanks. That’s been bugging me for a while. Play testers kept saying “It doesn’t make sense!!!! Volume this, volume that.”
    Not much on-line to help with this issue either.

  3. Tom
    April 7th, 2010 at 18:00 | #3

    Cheers for this tip, I was looking for something this simply explained in the android SDK!

  4. April 8th, 2010 at 13:01 | #4

    If you need to reset the volume control back to the default, there is one other value you can use:

    this.setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE);

  5. April 13th, 2010 at 16:35 | #5

    @Jarcikon, good tip, thanks

  6. July 7th, 2010 at 23:00 | #6

    Thanks so much! This has also been bugging me and there isn’t anything online I could find!

  7. July 28th, 2010 at 03:25 | #7

    Wow that’s easy.
    Thanks for the tip.
    First hit on Google.

  8. Sonja
    July 29th, 2010 at 10:26 | #8

    Thanks! And if you want to just have maximum media volume when your application is started, use

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

    in your onResume() (and good practie would be to turn it down in onPause()), where the last param “flags” (here 0) sets how this change should be indicated, 0 means “not”, AudioManager.FLAG_PLAY_SOUND for example plays some beep at the new volume level!

  9. Sonja
    July 29th, 2010 at 10:28 | #9

    Forgot to say, AudioManager.STREAM_MUSIC is the one that sets”Music/video volume”, might be helpful

  1. No trackbacks yet.
// unused langs // // // //