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.
To increase the volume you have to pass the
And to reduce the volume you have to pass the
I have created a small sample project to show the entire flow in action. You can download the project from my Github page.
- Recovering Android KeyStore password
- Using WebSockets with Android to control Arduino robots
- 10 more open source Android apps which every developer must look into
- Building Robots using Android and Arduino in Google Devfestx
- Transferring data from Android using Audio
- Building Robots using Arduino and Android at DroidCon