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
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.
Reduce Volume
And to reduce the volume you have to pass the AudioManager.ADJUST_LOWER
constant.
I have created a small sample project to show the entire flow in action. You can download the project from my Github page.
thank you for this very useful blog hope to see it grow bigger soon
sir,
i am not getting one thing that where to put the music files in emulator or where should i copy the music files in the code.