Controlling Sound Setting With AudioManager

Today I will explain you about how to use AudioManager Class in your Android applications which provides access to volume and ringer mode control.

We will see how to use AudioManager class in getting and setting the device’s volume such as Media player(Music), Alarm, System,Ringer etc as well as setting different ringer mode Silent, Vibrate and Normal of your device.

Before we start developing the application, you can get more details of AudioManager class here.

Setting the ringer mode pro-grammatically is very quick and easy, the only thing you need to do is a call to setRingerMode() method by passing the type of ringer mode as its parameter. The code below will set the ringer mode to Silent.

 

AudioManager mode = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
mode.setRingerMode(AudioManager.RINGER_MODE_SILENT);

 

Isn’t it very simple!!!!!!!!!

Now follow the steps below to create the application for system’s sound setting.

Step1: Create a new android project in your IDE (studio or eclipse any)

Step2: Create and write following into your layout:

 

<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:id="@+id/textView" />

<TextView android:text="Adjust Alarm Volume" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewAlarm"
android:layout_below="@+id/textView"/>
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/alarm"
android:layout_below="@+id/textViewAlarm"/>
<TextView android:text="Adjust Music Volume" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewMusic"
android:layout_below="@+id/alarm"/>
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/music"
android:layout_below="@+id/textViewMusic" />
<TextView android:text="Adjust Ringtone Volume" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewRing"
android:layout_below="@+id/music"/>
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ring"
android:layout_below="@+id/textViewRing" />
<TextView android:text="Adjust System Volume" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewSystem"
android:layout_below="@+id/ring"/>
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/system"
android:layout_below="@+id/textViewSystem"/>
<TextView android:text="Adjust Voice Volume" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewVoice"
android:layout_below="@+id/system"/>
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/voice"
android:layout_below="@+id/textViewVoice"/>
</RelativeLayout>

 

Setp3: Write following into your activity:

 

package com.example.systemvolumesettings;

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

public class MainActivity extends Activity {

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

mAudio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

SeekBar alarm = (SeekBar)findViewById(R.id.alarm);
SeekBar music = (SeekBar)findViewById(R.id.music);
SeekBar ring = (SeekBar)findViewById(R.id.ring);
SeekBar system = (SeekBar)findViewById(R.id.system);
SeekBar voice = (SeekBar)findViewById(R.id.voice);

initControls(alarm, AudioManager.STREAM_ALARM);
initControls(music, AudioManager.STREAM_MUSIC);
initControls(ring, AudioManager.STREAM_RING);
initControls(system, AudioManager.STREAM_SYSTEM);
initControls(voice, AudioManager.STREAM_VOICE_CALL);

}

private void initControls (SeekBar seek, final int stream)
{

seek.setMax(mAudio.getStreamMaxVolume(stream));
seek.setProgress(mAudio.getStreamVolume(stream));

seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
{
mAudio.setStreamVolume(stream, progress, AudioManager.FLAG_PLAY_SOUND);
}
public void onStartTrackingTouch(SeekBar bar) {
}
public void onStopTrackingTouch(SeekBar bar) {
}
});
}
@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);
}
}

 

 

Step4: Run to check output as shown below:

sound1

if you change the settings here you will see they are updated to the system as well. For example if you set the ringtone volume to minimum or 0 you can see the system will set on to silent mode immediately.

sound2

This way you can set this as a activity to control volumes in your game or application.

Checkout my Previouse blog on Ripple Animation.

Leave a Comment: