Android AudioManager Demo

Hello All. I have been doing some research on the Android AudioManager API, and thought that since their aren’t many tutorials out there on this topic, that I would create one.

I am assuming that you already have a basic understanding of Java, and you have already installing the Android SDK on your computer. I am not going over how to create a project from scratch, but rather give you the source code, and explain it in detail.

I have built this so it will work on my phone, which is running Android 2.1.

If you have any questions, or need any extra help, please leave a comment below.

User Interface
==========

I’m aware that this interface is VERY basic, and is pretty lame. But I didn’t want to spend too much time creating the GUI, I just needed something that I could test the 3 main audio profiles, Normal, Vibrate, and Silent.

Since this UI is very basic, I haven’t included the XML file. All I did was add 3 buttons using a relative layout view. If you need help creating 3 buttons, let me know and I will help you out.

Code
====

Now to the fun part. In order to change any of the sound settings on an Android device you must first import the media AudioManager package.

import android.media.AudioManager;

Then you have to create an instance of the AudioManager

AudioManager audio;

Then all you have to do is set up the listen handler for the buttons and use the AudioManager function setRingerMode and set it to 0(silent), 1(vibrate), or 2(normal).

public void onClick(View view) {

if (view == findViewById(R.id.btnNormal)){
normalClick();
} else if (view == findViewById(R.id.btnVibrate)) {
vibrateClick();
} else if (view == findViewById(R.id.btnSilent)) {
silentClick();
} else if (view == findViewById(R.id.btnStatus)){
statusClick();
}
}

public void normalClick() {
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.setRingerMode(2);
displayToastMsg("Normal");
}

public void vibrateClick() {
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.setRingerMode(1);
displayToastMsg("Vibrate");
}

public void silentClick() {
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.setRingerMode(0);
displayToastMsg("Silent");
}

So far, I have just given you the main segments of my code, I have also included a ‘view status’ button which simply displays a toast msg that tells the user what state their phone is currently in. Also, in every function I have called ‘displayToastMsg’ which is a simple function that displays what state the user has just switched their phone to.

Below is the entire source code from AudioBasic1.java

package matt.example.audioBasic1;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.media.AudioManager;
import android.widget.Toast;

public class AudioBasic1 extends Activity implements OnClickListener {
private AudioManager audio;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button normal = (Button) findViewById(R.id.btnNormal);
Button vibrate = (Button) findViewById(R.id.btnVibrate);
Button silent = (Button) findViewById(R.id.btnSilent);
Button status = (Button) findViewById(R.id.btnStatus);

normal.setOnClickListener(this);
vibrate.setOnClickListener(this);
silent.setOnClickListener(this);
status.setOnClickListener(this);

}

public void onClick(View view) {

if (view == findViewById(R.id.btnNormal)){
normalClick();
} else if (view == findViewById(R.id.btnVibrate)) {
vibrateClick();
} else if (view == findViewById(R.id.btnSilent)) {
silentClick();
} else if (view == findViewById(R.id.btnStatus)){
statusClick();
}
}

public void normalClick() {
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.setRingerMode(2);
displayToastMsg("Normal");
}

public void vibrateClick() {
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.setRingerMode(1);
displayToastMsg("Vibrate");
}

public void silentClick() {
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.setRingerMode(0);
displayToastMsg("Silent");
}

public void statusClick() {
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int status = audio.getRingerMode();
if(status == 0) {
displayToastMsg("Silent");
} else if (status == 1) {
displayToastMsg("Vibrate");
} else if (status == 2) {
displayToastMsg("Normal");
}
}

public void displayToastMsg(String status) {
String toastMsg = "Current Ringer Settings is " + status;
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
}
}

If you are interested in downloading my Eclipse project, you can click [here] and download it from mediafire.

I hope this has helped someone out there. Please leave your comments, and let me know what apps you’re making.

References
=======

Android API – http://developer.android.com/reference/packages.html

ToastMsg – http://developer.android.com/reference/android/widget/Toast.html

http://developer.android.com/guide/topics/ui/notifiers/toasts.html

AudioManager – http://developer.android.com/reference/android/media/AudioManager.html

– Matt

Advertisements

Posted on April 14, 2011, in Android, Development and tagged , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: