As stated in the title of this article let’s discuss how to adjust the volume of an Android Phone programmatically from the App. Basically, control the volume in the app mean
- Increase or Decrease the Volume without the Volume Bar UI
- Increase or Decrease the Volume with the Volume Bar UI
- Mute or Unmute the device
So we are going to discuss all these three processes step by step. Note that we are going to implement this project using the Kotlin language.
Increase or Decrease the Volume without the Volume Bar UI
To programmatically control the volume in an Android device, follow the following steps:
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Working with the activity_main.xml file
When the setup is ready, go to the activity_main.xml file, which represents the UI of the project. Create two Buttons, one for increasing the volume and the other for decreasing it, one below the other. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity" >
< Button
android:id = "@+id/btnUp"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerHorizontal = "true"
android:layout_above = "@+id/btnDown"
android:text = "+"
/>
< Button
android:id = "@+id/btnDown"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerInParent = "true"
android:text = "-"
/>
</ RelativeLayout >
|
Step 3: Working with the MainActivity.kt file
In the MainActivity.kt file, declare the two Buttons and an audio manager (refer to the codes). While setting the on click listeners to the buttons, we would use the audio manager to increase or decrease the volume. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.media.AudioManager
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val upBtn = findViewById<Button>(R.id.btnUp)
val downBtn = findViewById<Button>(R.id.btnDown)
val audioManager = applicationContext.getSystemService(AUDIO_SERVICE) as AudioManager
upBtn.setOnClickListener {
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND)
}
downBtn.setOnClickListener {
audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND)
}
}
}
|
Output: Run on Emulator
Increase or Decrease the Volume with the Volume Bar UI
The only change we have to make is to pass the parameter AudioManager.FLAG_SHOW_UI instead of AudioManager.FLAG_PLAY_SOUND in the MainActivity.kt file. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.content.Context
import android.media.AudioManager
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val upBtn = findViewById<Button>(R.id.btnUp)
val downBtn = findViewById<Button>(R.id.btnDown)
val audioManager = applicationContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager
upBtn.setOnClickListener {
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI)
}
downBtn.setOnClickListener {
audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI)
}
}
}
|
Output: Run on Emulator
It can be seen that the volume increases when “+” is clicked, and similarly, it decreases when “-” is clicked. When the phone becomes completely mute, changes can be seen on the volume bar.
Mute or Unmute the Device
Step 1: Working with the activity_main.xml file
When the setup is ready, go to the activity_main.xml file, which represents the UI of the project. Create two Buttons, one to mute and the other to unmute the device, one below the other. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity" >
< Button
android:id = "@+id/btnMute"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerHorizontal = "true"
android:text = "Mute"
android:layout_above = "@+id/btnUnmute"
/>
< Button
android:id = "@+id/btnUnmute"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerInParent = "true"
android:text = "Unmute"
/>
</ RelativeLayout >
|
Step 2: Working with the MainActivity.kt file
In the MainActivity.kt file, declare the two Buttons and an audio manager (refer to the codes). While setting the on click listeners to the buttons, we would use the audio manager to mute or unmute the device. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.content.Context
import android.media.AudioManager
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val muteBtn = findViewById<Button>(R.id.btnMute)
val unmuteBtn = findViewById<Button>(R.id.btnUnmute)
val audioManager = applicationContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager
muteBtn.setOnClickListener {
audioManager.adjustVolume(AudioManager.ADJUST_MUTE, AudioManager.FLAG_SHOW_UI)
}
unmuteBtn.setOnClickListener {
audioManager.adjustVolume(AudioManager.ADJUST_UNMUTE, AudioManager.FLAG_SHOW_UI)
}
}
}
|
Output: Run on Emulator
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
15 Oct, 2020
Like Article
Save Article