Thursday, February 26, 2015

Android Audio Manager New Tutorial

in  post, I will be discussing about using , AudioManager Class in Android applications.

with AudioManager Class provides access to volume and ringer mode control which was added in API level
I wrote separate post about using AudioManager in getting and setting the current device’s volume (Media player, , Notification etc) long back.

But in this post, I am going to discuss about using AudioManager in getting and setting the ringer mode (Silent, Vibrate and Normal).
What is Seekbar?
A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys.
Usage: Seekbar can be used to adjust media player volume, set brightness of the screen, seek a playing music etc.
What are we going to develop?
We will develop a simple application to demonstrate how it is used to set Media player, Ringer, Alarm and Notification volume for your device.


Quick Links.

  • Project Structure
  • Code Listings
  • Demo
  • Download Source Code

Project .Structure

Layout creation:
  • Create new android project [File >> New >> Android Project] with project name SeekBarExample
  • Click next and select target android device version [I chose version 2.2]
  • Click next and enter package name – ‘com.prgguru.android’
  • Click finish

Code Listings

Layout XML:
Open main.xml present under /res/layout folder and replace the XML with the below one.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Adjust Media player volume"
        android:textAppearance="?android:attr/textAppearanceLarge" android:padding="10px"/>
    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Adjust Ringer volume"
        android:textAppearance="?android:attr/textAppearanceLarge" android:padding="10px" />
    <SeekBar
        android:id="@+id/seekBar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />   
    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Adjust Alarm volume"
        android:textAppearance="?android:attr/textAppearanceLarge" android:padding="10px"/>
    <SeekBar
        android:id="@+id/seekBar3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Adjust Notification volume"
        android:textAppearance="?android:attr/textAppearanceLarge" android:padding="10px"/>
    <SeekBar
        android:id="@+id/seekBar4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
Application layout will look like:
Change to graphical layout of Main.xml, the layout design should look like below:

Create following objects under SeekBarExampleActivity class to refer seekbars and to get audimanager:
1
2
3
4
5
private SeekBar mediaVlmSeekBar = null;
private SeekBar ringerVlmSeekBar = null;
private SeekBar alarmVlmSeekBar = null;
private SeekBar notifyVlmSeekBar = null;
private AudioManager audioManager = null;
Suggests an audio stream whose volume should be changed by the hardware volume controls. Add following snippet inside onCreate method after ‘super.onCreate(savedInstanceState)’
1
2
3
4
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);       
this.setVolumeControlStream(AudioManager.STREAM_RING);   
this.setVolumeControlStream(AudioManager.STREAM_ALARM);
this.setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);
A separate method ‘initControls’ to handle the seekbars is going to be created, call it inside onCreate method:

1
initControls();
private void initControls() {
//Return the handle to a system-level service - 'AUDIO'.
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//Find the seekbar 1
mediaVlmSeekBar = (SeekBar) findViewById(R.id.seekBar1);
//Set the max range(Volume in this case) of seekbar
//for Media player volume
mediaVlmSeekBar.setMax(audioManager
        .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
//Set the progress with current Media Volume
mediaVlmSeekBar.setProgress(audioManager
        .getStreamVolume(AudioManager.STREAM_MUSIC));
//Find the seekbar 2
ringerVlmSeekBar = (SeekBar) findViewById(R.id.seekBar2);
//Set the max range(Volume in this case) of seekbar
//for Phone ringer volume
ringerVlmSeekBar.setMax(audioManager
        .getStreamMaxVolume(AudioManager.STREAM_RING));
//Set the progress with current Ringer Volume
ringerVlmSeekBar.setProgress(audioManager
        .getStreamVolume(AudioManager.STREAM_RING));
//Find the seekbar 3
alarmVlmSeekBar = (SeekBar) findViewById(R.id.seekBar3);
//Set the max range(Volume in this case) of seekbar
//for Alarm volume
alarmVlmSeekBar.setMax(audioManager
        .getStreamMaxVolume(AudioManager.STREAM_ALARM));
//Set the progress with current Alarm Volume
alarmVlmSeekBar.setProgress(audioManager
        .getStreamVolume(AudioManager.STREAM_ALARM));
//Find the seekbar 4
notifyVlmSeekBar = (SeekBar) findViewById(R.id.seekBar4);
//Set the max range(Volume in this case) of seekbar
//for Notification volume
notifyVlmSeekBar.setMax(audioManager
        .getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION));
//Set the progress with current Notification Volume
notifyVlmSeekBar.setProgress(audioManager
        .getStreamVolume(AudioManager.STREAM_NOTIFICATION));
try {
    //Listener to receive changes to the SeekBar1's progress level
    mediaVlmSeekBar
        .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
    public void onStopTrackingTouch(SeekBar arg0) {
        }
    public void onStartTrackingTouch(SeekBar arg0) {
        }
    //When progress level of seekbar1 is changed
    public void onProgressChanged(SeekBar arg0,
        int progress, boolean arg2) {
    audioManager.setStreamVolume(
        AudioManager.STREAM_MUSIC, progress, 0);
    }
 });
   //Listener to receive changes to the SeekBar2's progress level
   ringerVlmSeekBar
         .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
   public void onStopTrackingTouch(SeekBar arg0) {
        }
   public void onStartTrackingTouch(SeekBar arg0) {
        }
   //When progress level of seekbar2 is changed
   public void onProgressChanged(SeekBar arg0,
        int progress, boolean arg2) {
    audioManager.setStreamVolume(
        AudioManager.STREAM_RING, progress, 0);
     }
 });
   //Listener to receive changes to the SeekBar3's progress level
   alarmVlmSeekBar
        .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
   public void onStopTrackingTouch(SeekBar arg0) {
        }
   public void onStartTrackingTouch(SeekBar arg0) {
        }
   //When progress level of seekbar3 is changed
   public void onProgressChanged(SeekBar arg0,
        int progress, boolean arg2) {
     audioManager.setStreamVolume(
            AudioManager.STREAM_ALARM, progress, 0);
    }
 });
   //Listener to receive changes to the SeekBar4's progress level
   notifyVlmSeekBar
        .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
   public void onStopTrackingTouch(SeekBar arg0) {
        }
   public void onStartTrackingTouch(SeekBar arg0) {
        }
   //When progress level of seekbar4 is changed
   public void onProgressChanged(SeekBar arg0,
            int progress, boolean arg2) {
     audioManager.setStreamVolume(
        AudioManager.STREAM_NOTIFICATION, progress,0);
    }
 });
} catch (Exception e) {
    e.printStackTrace();
}
}


Demo

Let us test the application:
Run click on the project >> Run as >> Android application
You could see following screen:

0 comments :

Post a Comment

Blog Archive

increase adsense earning

Package
URL OR WEB ADDRESH

Live Traffic Feed