How do you control the audio in Android? We're not just talking about turning the volume up (but we like your sense of humour); it's so much more than that! If you've ever wondered what goes into controlling the audio of your system with Android Audio Manager and Audio Capture as an Android Developer, then wonder no more. In our latest tech tutorial, we'll design a demo application to implement the audio function and show you how to become a pro at Android. Let's crack on!
So what is Android Audio Manager?
When we get any notification on our phone, the device will play a ringtone. Whether it's a call, message or another system alert, an audio response is always generated (even if your phone is on Silent or Vibrate). These are all handled through the Android Audio Manager. In a nutshell, you can manage ringtones, ringer modes and volumes through this class. So what can you do as a developer to understand this more? We'll get into the basics below.
Using the functionality provided by the AudioManager class, you can adjust the volume easily. For instance, there is a setRingerMode() method used to change the ringer volume of your device. When you create an Audio Manager Class in your application, this is the method that is called and there are three different integer parameters that are passed in setRingerMode() method. These parameters serve the following purposes:
RINGER_MODE_NORMAL
When passed, this mode will set your device mode to normal or general mode.
RINGER_MODE_SILENT
When passed, this mode will set your device mode to silent mode.
RINGER_MODE_VIBRATE
When passed, this mode will set your device mode to vibrate mode.
To get a better understanding of how to set a device's audio, let's take a look at a code snippet for executing the AudioManager class:
AudioManager aManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
What are the Audio Manager Methods to use?
So far, we've only discussed the setRingerMode() method. There are other methods as well:
getRingerMode()
This method displays the device’s current ringing mode.
getMode()
This method tells us the current audio mode of the device.
getStreamVolume(int streamType)
This method brings the current volume index for a particular audio stream to your device.
getStreamMaxVolume(int streamtype)
This method tells us the maximum volume index a stream can have.
adjustVolume(int direction, int flags)
You can adjust your device’s volume using this method.
isMusicActive()
This is used to check if your device is playing music or not.
Let's try Audio Manager Implementation Through an App
MANIFEST.XML
After creating a new Android application with no activity, your app needs access to the Do Not Disturb mode. So add the permission below in your manifest file after the package name. It'll allow you to change the ringing modes.
(P.S. to avoid any conflict with your app, you just need to copy the <uses-permission> tag. The full code is just for your reference.)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.audio_manager">
<!-- Add the below permission to access the ringing modes-->
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.myTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ACTIVITY_MAIN.XML
After declaring permission in your manifest file, you'll need to create buttons to apply various ringing modes. Let's create one additional button to display the current mode. Copy the following code to add buttons to your activity_main.xml layout file and then continue.
<Button
android:id="@+id/normal_mode"
android:onClick="setNormalMode"
android:layout_marginTop="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Normal Mode"
android:textSize="18sp"
android:layout_gravity="center"
android:backgroundTint="#4CAF50" />
<Button
android:onClick="setVibrateMode"
android:id="@+id/vibrate_mode"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vibrate Mode"
android:textSize="18sp"
android:layout_gravity="center"
android:backgroundTint="#E91E63" />
<Button
android:onClick="setSilentMode"
android:id="@+id/silent_mode"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Silent Mode"
android:textSize="18sp"
android:backgroundTint="#FF5722" />
<Button
android:onClick="viewCurrentMode"
android:id="@+id/view_current_mode"
android:layout_marginTop="80dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Check Current Mode"
android:textSize="18sp"
android:backgroundTint="#3F51B5" />
MainActivity.kt
Firstly, add the following dependencies to your Main Activity Kotlin file:
import android.provider.Settings
import android.widget.Toast
import android.media.AudioManager
import android.content.Intent
import android.app.NotificationManager
If these dependencies show errors in your code, it means that they're not called in your application. Try to add them to your app-level build.gradle file first.
Inside your MainActivity.kt file, at the beginning of your MainActivity class, declare instances of NotificationManager and AudioManagers class like this:
lateinit var my_notification_manager:NotificationManager
lateinit var my_audio_manager:AudioManager
Inside your onCreate function, after setting the layout with setContentView, create an object of NotificationManager class.
my_notification_manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
Next, check if your Android version is Marshmallow or above. If it is, then you can give 'Do Not Disturb' access to your application. Once this is done, create an instance of AudioManager after that like this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !my_notification_manager.isNotificationPolicyAccessGranted) {
startActivity(Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS))
}
my_audio_manager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
After closing your onCreate function, create three new functions for normal, vibrate and silent modes. Create another function to view the current mode.
fun setNormalMode(view: View) {
my_audio_manager.ringerMode = AudioManager.RINGER_MODE_NORMAL
Toast.makeText(this@MainActivity, "Switched to Normal Mode", Toast.LENGTH_LONG).show() }
fun setVibrateMode(view: View) {
my_audio_manager.ringerMode = AudioManager.RINGER_MODE_VIBRATE
Toast.makeText(this@MainActivity, "Switched to Vibrate Mode", Toast.LENGTH_LONG).show() }
fun setSilentMode(view: View) {
my_audio_manager.ringerMode = AudioManager.RINGER_MODE_SILENT
Toast.makeText(this@MainActivity, "Switched to Silent Mode", Toast.LENGTH_LONG).show() }
fun viewCurrentMode(view: View) {
when(my_audio_manager.ringerMode) {
AudioManager.RINGER_MODE_NORMAL -> {
Toast.makeText(this@MainActivity, "Current Mode is Normal Mode", Toast.LENGTH_LONG).show() }
AudioManager.RINGER_MODE_VIBRATE -> {
Toast.makeText(this@MainActivity, "Current Mode is Vibrate Mode", Toast.LENGTH_LONG).show() }
AudioManager.RINGER_MODE_SILENT -> {
Toast.makeText(this@MainActivity, "Current Mode is Silent Mode", Toast.LENGTH_LONG).show()
}
}
}
After making all these changes, run your application. A dialogue box will appear to ask for the required permission and that's it! It really is as simple as that!
Conclusion
Congratulations on getting to grips with how to control the audio in Android. What's next? Well, have a go and let us know how you get on! If you're interested in more articles like this check out our Android articles or React Native articles.
Like what you've read or want more like this? Let us know! Email us here or DM us: Twitter, LinkedIn, Facebook, we'd love to hear from you.