Android AudioManager volume control example tutorial using seekbar

How to change,manage alarm,dtmf, music, notification, ring, system, voice call volume using AudioManager.

In this tutorial we are using android’s AudioManager package class which gives us the ability to control all the audio devices like alarm volume, audio stream of dtmf tones, media player music management, ringer volume controlling and voice call volume. So in this tutorial we are simply increasing and decreasing notification volume, ringer volume, alarm volume and media player volume on seekbar movement. So here is the complete step by step tutorial for Android AudioManager volume control example tutorial using seekbar.

android-project-download-code-button

Android AudioManager volume control example tutorial using seekbar.

Code for MainActivity.java file.

 package com.audiomanager_android_examples.com;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.widget.SeekBar;

public class MainActivity extends Activity {

 SeekBar alarm, mediaPlayer, ringer, notification ;
 AudioManager audioManager;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 alarm = (SeekBar)findViewById(R.id.seekBar1);
 mediaPlayer = (SeekBar)findViewById(R.id.seekBar2);
 ringer = (SeekBar)findViewById(R.id.seekBar3);
 notification = (SeekBar)findViewById(R.id.seekBar4);
 
 audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
 
 alarm.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM));
 
 mediaPlayer.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
 
 ringer.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_RING));
 
 notification.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION));
 
 
 
 alarm.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
 @Override
 public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
 
 audioManager.setStreamVolume(AudioManager.STREAM_ALARM, i, 0);
 }

 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {

 }

 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {

 }
 });
 
 
 mediaPlayer.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
 @Override
 public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
 
 audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, i, 0);
 }

 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {

 }

 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {

 }
 });
 
 
 ringer.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
 @Override
 public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
 
 audioManager.setStreamVolume(AudioManager.STREAM_RING, i, 0);
 }

 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {

 }

 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {

 }
 });
 
 
 notification.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
 @Override
 public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
 
 audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, i, 0);
 }

 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {

 }

 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {

 }
 });
 }
}

Code for activity_main.xml layout file.

 <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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.audiomanager_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="Set Alarm Volume"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:layout_marginTop="10dp" />

 <SeekBar
 android:id="@+id/seekBar1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="10dp" />

 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/seekBar1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="10dp"
 android:text="Set Media Player Volume"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <SeekBar
 android:id="@+id/seekBar2"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_below="@+id/textView2"
 android:layout_marginTop="10dp" />

 <TextView
 android:id="@+id/textView3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/seekBar2"
 android:layout_centerHorizontal="true"
 android:text="Set Ringer Volume"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:layout_marginTop="10dp" />

 <SeekBar
 android:id="@+id/seekBar3"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_centerVertical="true"
 android:layout_marginTop="10dp" />

 <TextView
 android:id="@+id/textView4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/seekBar3"
 android:layout_centerHorizontal="true"
 android:text="Set Notification Volume"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:layout_marginTop="10dp" />

 <SeekBar
 android:id="@+id/seekBar4"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_below="@+id/textView4"
 android:layout_marginTop="10dp" />

</RelativeLayout>

Screenshot:

Android AudioManager volume control example tutorial using seekbar

Click here to download Android AudioManager volume control example tutorial using seekbar project with source code.

3 Comments

  1. I opened the code in Android Studio. But it shows an error in the XML code. In this line :
    tools:context=”com.audiomanager_android_examples.com.MainActivity”
    Please help. Thanks.

  2. working perfectly

Leave a Reply

Your email address will not be published. Required fields are marked *