Android Audio Manager Tutorial

In this post, I will be discussing about using AudioManager Class in Android applications. AudioManager Class provides access to volume and ringer mode control which was added in API level 1.

I wrote separate post about using AudioManager in getting and setting the current device’s volume (Media player, Alarm, Notification etc) long back. (Read: Android Seekbar example – Set Device Volume)

But in this post, I am going to discuss about using AudioManager in getting and setting the ringer mode (Silent, Vibrate and Normal).

Before we start developing the application, take a quick look at the video demo of it.

Video Demo

From the video, I believe you got to know what the application will do.

About application

  • It is a simple application to set the ringer mode of device.

You can download source code from here if you don’t want to create Application from scratch, otherwise please proceed with below listings.

How to set Ringer mode?

It is very simple to set the ringer mode programmatically, all you need to do is invoke setRingerMode() method by passing the type of ringer mode as parameter.

Below code snippet will set the ringer mode to Silent.

AudioManager mode = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
mode.setRingerMode(AudioManager.RINGER_MODE_SILENT);

Okay, enough theory, let’s create application.

Step 1: Create Android Application Project

  • Create new android project [File >> New >> Android Application Project] with project name AudioManagerExample
  • Enter package name as ‘com.prgguru.example’ and activity name as ‘AudioManagerExample’
  • Choose Minimum Required SDK, Target SDK and Compile with. Confused on choosing these options? Take a look at Minimum Required SDK – Target SDK – Compile With post.
  • Click Next button and finally click ‘Finish’ to create project

Step 2: Add Colors in colors.xml present under values folder

<?xml version="1.0" encoding="UTF-8"?>
<resources >
    <color name="red">#d73249</color>
    <color name="yellow">#ffcb00</color>
     <color name="white">#fff</color>
</resources>

Step 3: Add drawable resources

Download below drawable resources (Right Click and Save) and add them to the drawable-mdpi folder present under res folder.

ic_action_vibrate ic_action_volume_muted ic_action_volume_on

Step 4: Design screen

Open activity_main.xml and replace it with below code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical" >

    <Button
        android:id="@+id/btnSilentMode"
        android:layout_width="250dip"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="5dp"
        android:background="@color/red"
        android:drawableLeft="@drawable/ic_action_volume_muted"
        android:onClick="makePhoneSilent"
        android:text="Activate Silent Mode"
        android:textColor="@color/white" >
    </Button>

    <Button
        android:id="@+id/btnRingerMode"
        android:layout_width="250dip"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnSilentMode"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="5dp"
        android:background="@color/red"
        android:drawableLeft="@drawable/ic_action_volume_on"
        android:onClick="makePhoneRinger"
        android:text="Activate Ringer Mode"
        android:textColor="@color/white" >
    </Button>

    <Button
        android:id="@+id/btnVibrateMode"
        android:layout_width="250dip"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnRingerMode"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="5dp"
        android:background="@color/red"
        android:drawableLeft="@drawable/ic_action_vibrate"
        android:onClick="makePhoneVibrate"
        android:text="Activate Vibrate Mode"
        android:textColor="@color/white" >
    </Button>

</RelativeLayout>

Step 5: AudioManagerExample.java – Main Class

Open AudioManagerExample.java and replace it with below code.

Each line of code is commented well, if you still have any doubt or question discuss it.

package com.prgguru.example;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;

public class AudioManagerExample extends Activity {
	// AudioManager Object
	AudioManager mode = null;
	// Buttons to change Ringer mode
	View ringerNormalBtn;
	View vibrateBtn;
	View silentBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// Instantiate Objects
		mode = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
		ringerNormalBtn = (View) findViewById(R.id.btnRingerMode);
		vibrateBtn = (View) findViewById(R.id.btnVibrateMode);
		silentBtn = (View) findViewById(R.id.btnSilentMode);

		//Get current mode of the phone
		int currentMode = mode.getRingerMode();

		// Check the current mode and change bgcolor of respective button to yellow
		// to just let user know the selected ringer mode
		if (currentMode == AudioManager.RINGER_MODE_NORMAL) {
			ringerNormalBtn.setBackgroundResource(R.color.yellow);
		} else if (currentMode == AudioManager.RINGER_MODE_SILENT) {
			silentBtn.setBackgroundResource(R.color.yellow);
		} else if (currentMode == AudioManager.RINGER_MODE_VIBRATE) {
			vibrateBtn.setBackgroundResource(R.color.yellow);
		}
	}

	// When 'Activate Silent Mode' button is clicked
	public void makePhoneSilent(View view) {
		// Set ringer mode to silent
		mode.setRingerMode(AudioManager.RINGER_MODE_SILENT);
		// Change ringer normal and vibrate buttons' bgcolor to red
		ringerNormalBtn.setBackgroundResource(R.color.red);
		vibrateBtn.setBackgroundResource(R.color.red);
		// Change Silent mode button's bgcolor to yellow
		view.setBackgroundResource(R.color.yellow);
	}

	// When 'Activate Ringer Mode' button is clicked
	public void makePhoneRinger(View view) {
		// Set ringer mode to ringer normal
		mode.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
		// Change silent and vibrate buttons' bgcolor to red
		silentBtn.setBackgroundResource(R.color.red);
		vibrateBtn.setBackgroundResource(R.color.red);
		// Change ringer normal button's bgcolor to yellow
		view.setBackgroundResource(R.color.yellow);
	}

	// When 'Activate Vibrate Mode' button is clicked
	public void makePhoneVibrate(View view) {
		// Set ringer mode to vibrate
		mode.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
		// Change ringer normal and silent buttons' bgcolor to red
		ringerNormalBtn.setBackgroundResource(R.color.red);
		silentBtn.setBackgroundResource(R.color.red);
		// Change vibrate button's bgcolor to yellow
		view.setBackgroundResource(R.color.yellow);
	}
}

[pglinkadssmall1]

Demo

That’s all. It’s time to test our code.

Run the application using emulator or device by right clicking on the project >> Run as >> Android applicaiton >> Choose emulator or device.


Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project (How to import android project in eclipse).

Download Source Code

*apk in Android  is the installation file similar to exe in windows.

[pglinkadssmall]

If you feel this article is helpful and interesting please spread a word about it to your friends and colleagues by sharing the article in Facebook or Twitter.

Share

You are always welcome to provide your comments and feedback from comment box.

[pgwriteforus]

[pgfeedback]

Reference: AudioManager

Related Articles

Author: Android Guru

Share This Post On
468 ad
  • adarshmadrecha

    Pls also post about managing images (like when we click on the add image to contact, it asks for which app to use for cropping and then contacts app uses that crpoed image and saves it), in this also add how to ensure that image size stays within limit.

  • Sure will post articles on Image cropping and saving. Thanks.

Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest

Shares