Android

Android Audio Manager

Android Audio Manager

In Android, you can control the audio of your device such as the volume and ringer mode of your device. To use this functionality, AudioManager class is used by calling the getSystemService() method. 

The example below will give you a clear understanding of how you can implement this in your android application. 

After creating an android application in your package com.firstapp.ashulakhwan.greatlearning, you need to modify some of your files such as MainActivity.java, activity_main.xml, strings.xml, and AndroidManifest.xml. And after modifying all these files, you can simply run the application. Let’s modify each file one by one as follows:

MainActivity.java 

package com.firstapp.ashulakhwan.greatlearning;

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

import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity {
   Button mode, general, vibrate;
   private AudioManager audioManager;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      vibrate=(Button)findViewById(R.id.button3);
      general=(Button)findViewById(R.id.button2);

      mode=(Button)findViewById(R.id.button);
      audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

      vibrate.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
            Toast.makeText(MainActivity.this,"Vibrate Mode ON",
               Toast.LENGTH_LONG).show();
         }
      });

      ring.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            Toast.makeText(MainActivity.this,"Sound Mode ON",
               Toast.LENGTH_LONG).show();
         }
      });

      mode.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            int mod=audioManager.getRingerMode();
            if(mod==AudioManager.RINGER_MODE_VIBRATE){
               Toast.makeText(MainActivity.this,"Vibrate Mode ON",
                  Toast.LENGTH_LONG).show();
            } else if(mod==AudioManager.RINGER_MODE_NORMAL){
               Toast.makeText(MainActivity.this,"Sound Mode ON",
                  Toast.LENGTH_LONG).show();
            } else {
               Toast.makeText(MainActivity.this,"Vibrate Mode ON",
                  Toast.LENGTH_LONG).show();
            }
         }
      });
   }
}

Now, file activity_main.xml will look like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
   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">

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Audio Manager"
      android:id="@+id/textView"
      android:textSize="34dp"
      android:layout_alignParentTop="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Great Learning"
      android:id="@+id/textView2"
      android:textColor="#ff3eff0f"
      android:textSize="35dp"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      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" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Ringer Mode"
      android:id="@+id/button"
      android:layout_below="@+id/imageView"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_marginTop="62dp" />

    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Vibrate Mode"
      android:id="@+id/button2"
      android:layout_alignTop="@+id/button2"
      android:layout_alignRight="@+id/textView"
      android:layout_alignEnd="@+id/textView" />

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Sound Mode"
      android:id="@+id/button3"
      android:layout_alignTop="@+id/button"
      android:layout_centerHorizontal="true" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Silent Mode"
      android:id="@+id/button4"
      android:layout_below="@+id/button2"
      android:layout_alignLeft="@+id/button2"
      android:layout_alignStart="@+id/button2" />     
</RelativeLayout>

Now we are going to modify the AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.firstapp.ashulakhwan.greatlearning" >

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.com.firstapp.ashulakhwan.greatlearning"
         android:label="@string/app_name" >
      
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      
      </activity>
      
   </application>
</manifest>

Finally, we’re done with all modifications and now you can run your application to see how it works. 

Top course recommendations for you

    Turbo C++
    1 hrs
    Beginner
    16.2K+ Learners
    4.41  (722)
    Dev C++ Tutorial
    1 hrs
    Beginner
    6.4K+ Learners
    4.45  (247)
    Hashing in Java
    2 hrs
    Intermediate
    6.5K+ Learners
    4.45  (202)
    Eclipse Tutorial
    1 hrs
    Beginner
    3K+ Learners
    4.51  (148)
    NoSQL Database
    2 hrs
    Beginner
    12.3K+ Learners
    4.51  (1022)
    SQL Triggers for Beginners
    1 hrs
    Intermediate
    13K+ Learners
    4.48  (615)
    WordPress with Docker
    1 hrs
    Beginner
    9.1K+ Learners
    4.52  (527)
    C++ Tutorial
    2 hrs
    Beginner
    49.4K+ Learners
    4.42  (3057)
    OOPs Concepts in C++
    2 hrs
    Beginner
    31.4K+ Learners
    4.42  (2167)
    Robotic Process Automation
    3 hrs
    Beginner
    13.3K+ Learners
    4.6  (759)