Android AudioManager with Examples

In android, AudioManager is a class which will provide an access to the volume and ringer modes of the device to adjust the volume or ringing modes (silent, vibration, ringing, etc.) based on our requirements.

 

To use AudioManager class in our application, we need to create an instance of AudioManager class by calling the getSystemService() method with an argument of Context.AUDIO_SERVICE.

 

Once we create an instance of AudioManager class, we can use setRingerMode() method to set the volume or ringing modes of our device based on our requirements.

 

Following is the syntax to use AudioManager class to set the volume or ringer modes (silent, ringing, vibration, etc) of our device in android applications.

 

AudioManager aManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

If you observe above code snippet, we create an instance of AudioManager class and setting the ringer mode as silent using setRingerMode() method.

 

We need to use following modes to set ringer mode as either normal (ringing) or vibration or silent in mobile device based on our requirements.

 

ModeDescription
RINGER_MODE_NORMAL This mode is used to set ringing mode in the device.
RINGER_MODE_SILENT This mode is used to set a silent mode in the device.
RINGER_MODE_VIBRATE This mode is used to set vibration mode in the device.

In android, by using AudioManager class getRingerMode() method we can easily get the device current ringer mode.

 

Following is the syntax of getting the device current ringer mode in the android application.

 

AudioManager aManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

int currentMode = aManager.getRingerMode();

if(currentMode == AudioManager.RINGER_MODE_NORMAL){
   
// Do your code
}

Now we will see how to change the device ringing mode to either ringing or silent or vibration on button click in android application with examples.

Android AudioManager Example

Following is the example of changing the device ringing mode to either ringing or silent or vibration on button click in the android application.

 

Create a new android application using android studio and give names as AudioManagerExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.

 

Once we create an application, open colors.xml file from /res/values folder path and add the color properties like as shown below.

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <
color name="colorPrimary">#3F51B5</color>
    <
color name="colorPrimaryDark">#303F9F</color>
    <
color name="colorAccent">#FF4081</color>
    <
color name="colorWhite">#FFFFFF</color>
</
resources>

Now open activity_main.xml file from \res\layout folder path and write the code like as shown below.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:orientation="vertical" android:layout_width="match_parent"
   
android:layout_height="match_parent">
    <
Button
       
android:id="@+id/btnRing"
       
android:layout_width="150dp"
       
android:layout_height="wrap_content"
       
android:layout_marginLeft="130dp"
       
android:layout_marginTop="150dp"
       
android:background="@color/colorPrimary"
       
android:layout_marginBottom="5dp"
       
android:text="Ring Mode"
       
android:textColor="@color/colorWhite" />
    <
Button
       
android:id="@+id/btnSilent"
       
android:layout_width="150dp"
       
android:layout_height="wrap_content"
       
android:layout_marginLeft="130dp"
       
android:background="@color/colorPrimary"
       
android:layout_marginBottom="5dp"
       
android:text="Silent Mode"
       
android:textColor="@color/colorWhite" />
    <
Button
       
android:id="@+id/btnVibrate"
       
android:layout_width="150dp"
       
android:layout_height="wrap_content"
       
android:layout_marginLeft="130dp"
       
android:background="@color/colorPrimary"
       
android:text="Vibrate Mode"
       
android:textColor="@color/colorWhite" />
</
LinearLayout>

Now open your main activity file MainActivity.java from \java\com.tutlane.audiomanagerexample path and write the code like as shown below

MainActivity.java

package com.tutlane.audiomanagerexample;
import android.content.Context;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button
silentbtn, vibratebtn, ringbtn;
    AudioManager
aManager;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
       
silentbtn = (Button)findViewById(R.id.btnSilent);
       
ringbtn = (Button)findViewById(R.id.btnRing);
       
vibratebtn = (Button)findViewById(R.id.btnVibrate);
       
aManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
       
int currentMode = aManager.getRingerMode();
       
if(currentMode == AudioManager.RINGER_MODE_NORMAL)
           
ringbtn.setBackgroundResource(R.color.colorAccent);
       
else if(currentMode == AudioManager.RINGER_MODE_SILENT)
           
silentbtn.setBackgroundResource(R.color.colorAccent);
       
else if(currentMode == AudioManager.RINGER_MODE_VIBRATE)
           
vibratebtn.setBackgroundResource(R.color.colorAccent);
       
silentbtn.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                   
aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                    Toast.makeText(getApplicationContext(),
"Slient Mode Activated",Toast.LENGTH_SHORT).show();
                   
ringbtn.setBackgroundResource(R.color.colorPrimary);
                   
silentbtn.setBackgroundResource(R.color.colorAccent);
                   
vibratebtn.setBackgroundResource(R.color.colorPrimary);
            }
        });
       
ringbtn.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
               
aManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                Toast.makeText(getApplicationContext(),
"Ringing Mode Activated",Toast.LENGTH_SHORT).show();
               
ringbtn.setBackgroundResource(R.color.colorAccent);
               
silentbtn.setBackgroundResource(R.color.colorPrimary);
               
vibratebtn.setBackgroundResource(R.color.colorPrimary);
            }
        });
       
vibratebtn.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
               
aManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                Toast.makeText(getApplicationContext(),
"Vibration Mode Activated",Toast.LENGTH_SHORT).show();
                
ringbtn.setBackgroundResource(R.color.colorPrimary);
               
silentbtn.setBackgroundResource(R.color.colorPrimary);
               
vibratebtn.setBackgroundResource(R.color.colorAccent);
            }
        });
    }
}

If you observe above code, we used setRingerMode() method of AudioManager class to set the ringer profiles based on our requirements.

Output of Android AudioManager Example

When we run the above program in the android studio we will get the result as shown below.

 

Android Audio Manager Example Result

 

If you observe the above example, initially the device is in Ringing Mode when we click on the Vibrate Mode button, the device mode will be changed to Vibrate and when we click on Silent Mode button, the device mode will be changed to Silent.

 

This is how we can use audiomanager in our android applications to control the ringer volume and ringer profiles (ring, silent, vibrate, etc.) based on our requirements.