How to play sound even when an Android phone is in silent mode ?


Introduction

Many times in android applications we come across a scenario where we have to play a notification sound to be played in the user's mobile device even when the device is in silent mode. In this article we will take a look at How to play a sound even when an Android phone is in silent mode.

Implementation

We will be creating a simple application in which we will be displaying a simple text view for displaying the heading of our application. After that we will be creating a button which we will be using to play the notification sound even when the device is in silent mode.

Step 1 − Creating a new project in Android Studio

Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project.

After clicking on New Project you will get to see the below screen.

Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.

Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.

Note − Make sure to select the Language as Kotlin.

After specifying all the details click on Finish to create a new Android studio project.

Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.java file.

Step 2 : Working with activity_main.xml

Navigate to activity_main.xml. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>activity_main.xml to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

<?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:id="@+id/idRLLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context=".MainActivity">

   <!-- text view for displaying  heading of the application -->
   <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_above="@id/idBtnPlayNotification"
      android:layout_centerInParent="true"
      android:layout_margin="10dp"
      android:padding="5dp"
      android:text="Play Notification when Android Phone is in Silent Mode"
      android:textAlignment="center"
      android:textAllCaps="false"
      android:textColor="@color/black"
      android:textSize="18sp"
      android:textStyle="bold" />

   <!-- on below line we are creating a button to play audio notification -->
   <Button
      android:id="@+id/idBtnPlayNotification"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:layout_margin="20dp"
      android:text="Play Notification"
      android:textAllCaps="false" />
</RelativeLayout>

Explanation − In the above code the root element is a Relative layout in android. This layout is a view group which is used to align all the elements within it relative to each other. We can relatively align all elements within Relative Layout with the help of ids or positions.

Inside this relative layout we are firstly creating a text view which we will be using to display the heading of our application. After that we are creating a button which is used to play the notification sound when the device is in silent mode.

At last we are adding a closing tag for our Relative Layout as the text view and button is enclosed in our relative layout.

Step 3 : Working with MainActivity.java

Navigate to MainActivity.java. If this file is not visible. To open this file. In the left pane navigate to app>java>your app’s package name>MainActivity.java to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

package com.example.androidjavaapp;

import android.content.Context;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

   // on below line creating a variable for button.
   private Button playNotificationBtn;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      // on below line we are initializing variables.
      playNotificationBtn = findViewById(R.id.idBtnPlayNotification);

      // on below line we are adding click listener for  button.
      playNotificationBtn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            // on below line we are creating an intent for send action.
            playNotificationSoundInSilentMode();
         }
      });
   }
   
   // on below line creating a method to play notification.
   public void playNotificationSoundInSilentMode() {
      
      // on below line creating and initializing variable for audio manager.
      AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
      
      // on below line getting original volume.
      int originalVolume = am.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
      
      // on below line setting stream volume.
      am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, am.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0);
      
      // on below line getting notification uri.
      Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
      
      // on below line creating a variable for ringtone and playing it.
      Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
      r.play();
      
      // on below line calling a handler function to again switch back to silent mode.
      Handler handler = new Handler();
      handler.postDelayed(new Runnable() {
         @Override
         public void run() {
            
            // on below line setting stream volumne.
            am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, originalVolume, 0);
         }
      }, 500);
   }
}

Explanation − In the above code for MainActivity.java file. Firstly we are creating a variable for our button.

Now we will get to see the onCreate method. This is the default method of every android application. This method is called when the application view is created. Inside this method we are setting the content view i.e the layout file named activity_main.xml to set the UI from that file.

After specifying the view, we are initializing the variable for our button. After that we added a click listener for our button to play the notification sound.

Inside the onclick method we are calling a method which will play the notification sound. Inside this method we are firstly changing the device's silent mode to general mode and then we are playing the notification sound by calling the Uri. After that we are again switching the device profile to silent mode.

After adding the above code now we have to simply click on the green icon in the top bar to run our application on a mobile device.

Note − Make sure you are connected to your real device or emulator.

Output

Conclusion

In the above tutorial we have taken a look at How to play a notification sound even when the Android phone is in silent mode.

Updated on: 30-Mar-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements