Audio Manager

Course- Android >

You can easily control your Ringer Volume and Ringer Profiles I-A: (mute, vibration, emphasis e.t.c) in Android. The Android Audio Manager provides classes that provide access to these controls.

To use the Android Manager Class, you must first call the GetSystemService () method to create an audio manager class object. Its syntax is given below


private AudioManager myAudioManager;
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);  

When you instantiate the object of the Audio Manager class, you can use the SettingRoad method to set your device's audio or bell profile. Its syntax is given below


myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

Method SetringRod takes an integer number as a parameter. For each mode, an integer number is assigned which will differentiate between different modes. Are possible modes

Sr.No Mode & Description
1 RINGER_MODE_VIBRATE

This Mode sets the device at vibrate mode.

2 RINGER_MODE_NORMAL

This Mode sets the device at normal(loud) mode.

3 RINGER_MODE_SILENT

This Mode sets the device at silent mode.

Once you have set the mode , you can call the getRingerMode() method to get the set state of the system. Its syntax is given below.


int mod = myAudioManager.getRingerMode();

Apart from the getRingerMode method, there are other methods available in the AudioManager class to control the volume and other modes. They are listed below.

Sr.No Method & description
1 adjustVolume(int direction, int flags)

This method adjusts the volume of the most relevant stream

2 getMode()

This method returns the current audio mode

3 getStreamMaxVolume(int streamType)

This method returns the maximum volume index for a particular stream

4 getStreamVolume(int streamType)

This method returns the current volume index for a particular stream

5 isMusicActive()

This method checks whether any music is active.

6 startBluetoothSco()

This method Start bluetooth SCO audio connection

7 stopBluetoothSco()

This method stop bluetooth SCO audio connection.

Example

The below example demonstrates the use of AudioManager class. It crates a application that allows you to set different ringer modes for your device.

To experiment with this example , you need to run this on an actual device.

Steps Description
1 You will use Android studio IDE to create an Android application under a package com.example.sairamkrishna.myapplication; While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.
2 Modify src/MainActivity.java file to add AudioManager code
3 Modify layout XML file res/layout/activity_main.xml add any GUI component if required.
4 Modify res/values/string.xml file and add necessary string components.
5 Modify AndroidManifest.xml to add necessary permissions.
6 Run the application and choose a running android device and install the application on it and verify the results.

Here is the content of src/MainActivity.java


package com.example.sairamkrishna.myapplication;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;

import android.os.Bundle;
import android.os.Environment;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;


public class MainActivity extends Activity {
   Button mode,ring,vibrate,silent;
   private AudioManager myAudioManager;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      vibrate=(Button)findViewById(R.id.button3);
      ring=(Button)findViewById(R.id.button2);
      
      mode=(Button)findViewById(R.id.button);
      silent=(Button)findViewById(R.id.button4);
      myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
      
      vibrate.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
            Toast.makeText(MainActivity.this,"Now in Vibrate Mode",Toast.LENGTH_LONG).show();
         }
      });
      
      ring.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            Toast.makeText(MainActivity.this,"Now in Ringing Mode",Toast.LENGTH_LONG).show();
         }
      });
      
      silent.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            Toast.makeText(MainActivity.this,"Now in silent Mode",Toast.LENGTH_LONG).show();
         }
      });
      
      mode.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            int mod=myAudioManager.getRingerMode();
            
            if(mod==AudioManager.RINGER_MODE_VIBRATE){
               Toast.makeText(MainActivity.this,"Now in Vibrate Mode",Toast.LENGTH_LONG).show();
            }
            
            else if(mod==AudioManager.RINGER_MODE_NORMAL){
               Toast.makeText(MainActivity.this,"Now in Ringing Mode",Toast.LENGTH_LONG).show();
            }
            
            else
            {
               Toast.makeText(MainActivity.this,"Now in Vibrate Mode",Toast.LENGTH_LONG).show();
            }
         }
      });
   }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
   }
   
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      
      int id = item.getItemId();
      
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
}

Here is the content of activity_main.xml


 xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

   
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Android Audio Recording"
      android:id="@+id/textView"
      android:textSize="30dp"
      android:layout_alignParentTop="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true" />
      
   
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Fastread"
      android:id="@+id/textView2"
      android:textColor="#ff3eff0f"
      android:textSize="35dp"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true" />
      
   
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/logo"
      android:layout_below="@+id/textView2"
      android:layout_alignLeft="@+id/textView2"
      android:layout_alignStart="@+id/textView2"
      android:layout_alignRight="@+id/textView2"
      android:layout_alignEnd="@+id/textView2" />
      
   
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Mode"
      android:id="@+id/button"
      android:layout_below="@+id/imageView"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_marginTop="59dp" />
      
   
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Ring"
      android:id="@+id/button2"
      android:layout_alignTop="@+id/button"
      android:layout_centerHorizontal="true" />
      
   
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="vibrate"
      android:id="@+id/button3"
      android:layout_alignTop="@+id/button2"
      android:layout_alignRight="@+id/textView"
      android:layout_alignEnd="@+id/textView" />
      
   
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Silent"
      android:id="@+id/button4"
      android:layout_below="@+id/button2"
      android:layout_alignLeft="@+id/button2"
      android:layout_alignStart="@+id/button2" />
      

Here is the content of Strings.xml


    name="app_name">My Application
    name="hello_world">Hello world!
    name="action_settings">Settings

Here is the content of AndroidManifest.xml


xml version="1.0" encoding="utf-8"?>
 xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.audiomanager"
   android:versionCode="1"
   android:versionName="1.0" >

   
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      
         android:name="com.example.sairamkrishna.myapplication"
         android:label="@string/app_name" >
      
         
             android:name="android.intent.action.MAIN" />
             android:name="android.intent.category.LAUNCHER" />
         
    

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Android Studio, open one of your project's activity files and click Run icon from the tool bar. Android studio will display a screen

Now select silent button, you would get silent icon at Notification bar