Adjusting the volume in Android through code

For my iAndroidRemote project, I had to adjust the volume of my Android phone. I found the code after digging around a bit.

I thought of sharing the code here, so that it would be useful for others and also I would know where to look for it when I need it for the next time.

Changing the volume in Android is pretty easy. You just need to know which class and method to call.

Getting the AudioManager Instance

First you have to get an instance of the AudioManager. You can get it by calling the getSystemService method of the Context object. If you are inside an Activity, you can get the instance by making the following code

public
Last updated

Hm.

This gist can't be viewed at the moment. Please send a message to Support and we'll have a look as soon as we can.

Something went wrong with that request. Please try again.

Once you have the AudioManager instance, all you have to do is to call the adjustVolume method with appropriate arguments.

Increase Volume

To increase the volume you have to pass the AudioManager.ADJUST_RAISE constant.

public
Last updated

Hm.

This gist can't be viewed at the moment. Please send a message to Support and we'll have a look as soon as we can.

Something went wrong with that request. Please try again.

Reduce Volume

And to reduce the volume you have to pass the AudioManager.ADJUST_LOWER constant.

public
Last updated

Hm.

This gist can't be viewed at the moment. Please send a message to Support and we'll have a look as soon as we can.

Something went wrong with that request. Please try again.

I have created a small sample project to show the entire flow in action. You can download the project from my Github page.

Related posts

Tags: ,

2 Comments so far

Trackback URI | Follow up comments through RSS Feed | Post a comment

1 Tweetbacks so far

2 Trackbacks/Pingbacks so far

Leave a Reply