Android Audio Manager and Volume Control Adjustment

In this android tutorial, we are going to learn how to use android Audio Manager and AUDIO_SERVICE to get android device sound mode and volume control adjustment.

We can use a simple android app to test and adjust our android device volume control.

We are going to a layout file that will have two sections – in the first section we will implement android sound mode and in the second section we will implement android sound and volume control.

We are going to make use of different View widgets like RadioButton, RadioGroup and SeekBar.

Before we dive into more details, it is important for us to understand what we are planning to achieve. Below is the screen-shot of the application we will be creating.

android audio manager

Lets start to soil our hands in code. Start up your IDE. For this tutorial, I am using the following tools and environment, feel free to use what works for you.

Windows 7

Android Studio

Sony Xperia ZL

Min SDK 14

Target SDK 23

To create a new android application project, follow the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidSoundTest

Package: com.inducesmile.androidsoundtest

Select Blank Activity

Name the Activity SoundTestActivity

Keep other default selections

Continue to click on next button until Finish button is active, then click on Finish Button.

LETS START WITH OUR PROJECT

Let update the build.gradle with this line of code

compile 'com.android.support:cardview-v7:23.0.+'

Open our main layout file – activity_sound_test located at the res > layout folder in you development environment. Add the following code inside the layout file.

<ScrollView 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="8dp"
    android:paddingRight="8dp"
    android:scrollbars="none"
    tools:context="inducesmile.com.completeandroid.SoundTestActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/vibration_informatiom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:background="@color/light_primary_color"
                android:textSize="16dp"
                android:textStyle="bold"
                android:textColor="@color/primary_text"
                android:text="@string/vibration_control"/>
            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/card_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:elevation="8dp"
                android:layout_marginBottom="8dp"
                android:layout_below="@+id/vibration_informatiom"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="8dp"
                card_view:cardUseCompatPadding="true">
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:padding="8dp">
                    <TextView
                        android:id="@+id/vibration"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/primary_text"
                        android:layout_alignParentTop="true"
                        android:layout_marginTop="8dp"
                        android:text="@string/vibrate"
                        android:layout_alignParentLeft="true"/>
                    <RadioGroup
                        android:id="@+id/vibrate_control"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:layout_alignParentLeft="true"
                        android:orientation="vertical"
                        android:layout_below="@+id/vibration">
                        <RadioButton
                        android:id="@+id/normal_mode"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/primary_text"
                        android:padding="8dp"
                        android:text="@string/normal_mode"/>
                        <RadioButton
                            android:id="@+id/silent_mode"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/primary_text"
                            android:padding="8dp"
                            android:text="@string/silent_mode"/>
                        <RadioButton
                            android:id="@+id/vibrate_mode"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/primary_text"
                            android:padding="8dp"
                            android:text="@string/vibrate_mode"/>
                    </RadioGroup>
                </RelativeLayout>
            </android.support.v7.widget.CardView>
            <TextView
                android:id="@+id/sound_informatiom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:background="@color/light_primary_color"
                android:textSize="16dp"
                android:textStyle="bold"
                android:layout_below="@+id/card_view"
                android:layout_marginTop="16dp"
                android:layout_alignParentLeft="true"
                android:textColor="@color/primary_text"
                android:text="@string/sound_control"/>
            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/card_view_two"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:elevation="8dp"
                android:layout_marginBottom="8dp"
                android:layout_below="@+id/sound_informatiom"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="8dp"
                card_view:cardUseCompatPadding="true">
                <RelativeLayout
                    android:id="@+id/one"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:padding="8dp">
                    <TextView
                        android:id="@+id/voice"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/primary_text"
                        android:layout_alignParentTop="true"
                        android:layout_marginTop="8dp"
                        android:text="@string/voice_control"
                        android:layout_alignParentLeft="true"/>
                    <SeekBar
                        android:id="@+id/voice_control"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:layout_centerHorizontal="true"
                        android:layout_below="@+id/voice"/>
                    <TextView
                        android:id="@+id/music"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/primary_text"
                        android:layout_below="@+id/voice_control"
                        android:layout_marginTop="24dp"
                        android:text="@string/music_control"
                        android:layout_alignParentLeft="true"/>
                    <SeekBar
                        android:id="@+id/music_control"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:layout_centerHorizontal="true"
                        android:layout_below="@+id/music"/>
                    <TextView
                    android:id="@+id/ringing"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/primary_text"
                    android:layout_below="@+id/music_control"
                    android:layout_marginTop="24dp"
                    android:text="@string/ring_control"
                    android:layout_alignParentLeft="true"/>
                    <SeekBar
                        android:id="@+id/ringing_control"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:layout_centerHorizontal="true"
                        android:layout_below="@+id/ringing"/>
                    <TextView
                        android:id="@+id/alarm"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/primary_text"
                        android:layout_below="@+id/ringing_control"
                        android:layout_marginTop="24dp"
                        android:text="@string/alarm_control"
                        android:layout_alignParentLeft="true"/>
                    <SeekBar
                        android:id="@+id/alarm_control"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:layout_centerHorizontal="true"
                        android:layout_below="@+id/alarm"/>
                    <TextView
                        android:id="@+id/system_volume"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/primary_text"
                        android:layout_below="@+id/alarm_control"
                        android:layout_marginTop="24dp"
                        android:text="@string/system_control"
                        android:layout_alignParentLeft="true"/>
                    <SeekBar
                        android:id="@+id/system_control"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:layout_centerHorizontal="true"
                        android:layout_below="@+id/system_volume"/>
                </RelativeLayout>
            </android.support.v7.widget.CardView>
        </RelativeLayout>
    </RelativeLayout>
</ScrollView>

This layout file comprises of a ScrollView root layout with a single RelativeLayout child. The RelativeLayout contains other View widget.

You can see that we made use of the CardView so we are going to update our app’s build.gradle file to include a dependency support library for CardView widget.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.android.support:cardview-v7:23.0.+'
}

Now, we will move over SoundTestActivity class. Open this file in your IDE, we will get the instances of our view controls

rGroup = (RadioGroup)findViewById(R.id.vibrate_control);
normalMode = (RadioButton)findViewById(R.id.normal_mode);
silentMode = (RadioButton)findViewById(R.id.silent_mode);
vibrateMode = (RadioButton)findViewById(R.id.vibrate_mode);

voice = (SeekBar)findViewById(R.id.voice_control);
music = (SeekBar)findViewById(R.id.music_control);
ring = (SeekBar)findViewById(R.id.ringing_control);
alarm = (SeekBar)findViewById(R.id.alarm_control);
system = (SeekBar)findViewById(R.id.system_control);

The getSystemService() method of the Activity class is called with AUDIO_SERVICE as parameter and it returns an instance of the audio manager.

The AudioManager object methods and properties are used to manipulate the sound features in android device.

Add the complete code below to SoundTextActivity.

import android.media.AudioManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
public class SoundTestActivity extends AppCompatActivity {
    private SeekBar voice = null;
    private SeekBar music = null;
    private SeekBar ring = null;
    private SeekBar alarm = null;
    private SeekBar system = null;
    private AudioManager mgr = null;
    private RadioGroup rGroup;
    private RadioButton normalMode;
    private RadioButton silentMode;
    private RadioButton vibrateMode;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sound_test);
        mgr=(AudioManager)getSystemService(getApplicationContext().AUDIO_SERVICE);
        rGroup = (RadioGroup)findViewById(R.id.vibrate_control);
        normalMode = (RadioButton)findViewById(R.id.normal_mode);
        silentMode = (RadioButton)findViewById(R.id.silent_mode);
        vibrateMode = (RadioButton)findViewById(R.id.vibrate_mode);
        voice = (SeekBar)findViewById(R.id.voice_control);
        music = (SeekBar)findViewById(R.id.music_control);
        ring = (SeekBar)findViewById(R.id.ringing_control);
        alarm = (SeekBar)findViewById(R.id.alarm_control);
        system = (SeekBar)findViewById(R.id.system_control);
        initBar(alarm, AudioManager.STREAM_ALARM);
        initBar(music, AudioManager.STREAM_MUSIC);
        initBar(ring, AudioManager.STREAM_RING);
        initBar(system, AudioManager.STREAM_SYSTEM);
        initBar(voice, AudioManager.STREAM_VOICE_CALL);
        setAudioMode();
        rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int index = group.indexOfChild(findViewById(group.getCheckedRadioButtonId()));
                System.out.println("Selected Button id " + index);
                if (checkedId == R.id.normal_mode) {
                    mgr.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                    normalMode.setChecked(true);
                }
                if (checkedId == R.id.silent_mode) {
                    mgr.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                    silentMode.setChecked(true);
                }
                if (checkedId == R.id.vibrate_mode) {
                    mgr.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                    vibrateMode.setChecked(true);
                }
            }
        });
    }
    private void setAudioMode(){
        System.out.println("Current Ringing Mode " + mgr.getMode());
        if(mgr.getRingerMode() == 0){
            mgr.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            normalMode.setChecked(true);
        }
        if(mgr.getMode() == 1){
            mgr.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            silentMode.setChecked(true);
        }
        if(mgr.getMode() == 2){
            mgr.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
            vibrateMode.setChecked(true);
        }
    }
    @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_sound_test, 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);
    }
    private void initBar(SeekBar bar, final int stream) {
        bar.setMax(mgr.getStreamMaxVolume(stream));
        bar.setProgress(mgr.getStreamVolume(stream));
        bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
                mgr.setStreamVolume(stream, progress, AudioManager.FLAG_PLAY_SOUND);
            }
            public void onStartTrackingTouch(SeekBar bar) {
            }
            public void onStopTrackingTouch(SeekBar bar) {
            }
        });
    }
}

This brings us to the end of this tutorial. I hope you have learned something new.

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email address so that you will be among the first to receive my new post once it is published.

Please if you love this tutorial, kindly download my android app – Complete Mathematics – in Google Play Store and let me know what you think about it.

My Source Code App Store

I have a complete Android login source codes with advance features and admin back-end with user login details that comprises of Android Facebook Login, Android Twitter Login, Android Google Plus Login, Android Login and Registration with Php and MySql, Android Login with Firebase. It sells for $3.

Kindly contact me if you need it. I offer 24 – 7 support for smooth integration in your application.

One Response

Add a Comment