Android--AudioManager控制音量
首先,让我们来看下本程序的实现截图:图中的五个ImageButton实现不同的功能:
1.调大音量
2.减小音量
3.调整手机模式为正常
4.调整手机模式为静音
5.调整手机模式为震动
下面给出实现的代码:
[java]
public class EX07_08 extends Activity {
/* 变量宣告 */
private ImageView myImage;
private ImageButton downButton;
private ImageButton upButton;
private ImageButton normalButton;
private ImageButton muteButton;
private ImageButton vibrateButton;
private ProgressBar myProgress;
private AudioManager audioMa;
private int volume=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 物件初始化 */
audioMa = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
myImage = (ImageView)findViewById(R.id.myImage);
myProgress = (ProgressBar)findViewById(R.id.myProgress);
downButton = (ImageButton)findViewById(R.id.downButton);
upButton = (ImageButton)findViewById(R.id.upButton);
normalButton = (ImageButton)findViewById(R.id.normalButton);
muteButton = (ImageButton)findViewById(R.id.muteButton);
vibrateButton = (ImageButton)findViewById(R.id.vibrateButton);
/* 设定初始的手机音量 */
volume=audioMa.getStreamVolume(AudioManager.STREAM_RING);
myProgress.setProgress(volume);
/* 设定初始的声音模式 */
int mode=audioMa.getRingerMode();
if(mode==AudioManager.RINGER_MODE_NORMAL) {
myImage.setImageDrawable(getResources() .getDrawable(R.drawable.normal));
}
else if(mode==AudioManager.RINGER_MODE_SILENT) {
myImage.setImageDrawable(getResources() .getDrawable(R.drawable.mute));
}
else if(mode==AudioManager.RINGER_MODE_VIBRATE) {
myImage.setImageDrawable(getResources() .getDrawable(R.drawable.vibrate));
}
/* 音量调小声的Button */
downButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
/* 设定音量调小声一格 */
audioMa.adjustVolume(AudioManager.ADJUST_LOWER, 0);
volume=audioMa.getStreamVolume(AudioManager.STREAM_RING);
myProgress.setProgress(volume);
/* 设定调整后声音模式 */
int mode=audioMa.getRingerMode();
if(mode==AudioManager.RINGER_MODE_NORMAL) {
myImage.setImageDrawable(getResources() .getDrawable(R.drawable.normal));
}
else if(mode==AudioManager.RINGER_MODE_SILENT) {
myImage.setImageDrawable(getResources() .getDrawable(R.drawable.mute));
}
else if(mode==AudioManager.RINGER_MODE_VIBRATE) {
myImage.setImageDrawable(getResources() .getDrawable(R.drawable.vibrate));
}
}
});
/* 音量调大声的Button */
upButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
/* 设定音量调大声一格 */
audioMa.adjustVolume(AudioManager.ADJUST_RAISE, 0);
volume=audioMa.getStreamVolume(AudioManager.STREAM_RING);
myProgress.setProgress(volume);
/* 设定调整后的声音模式 */
int mode=audioMa.getRingerMode();
if(mode==AudioManager.RINGER_MODE_NORMAL) {
myImage.setImageDrawable(getResources() .getDrawable(R.drawable.normal));
}
else if(mode==AudioManager.RINGER_MODE_SILENT) {
myImage.setImageDrawable(getResources() .getDrawable(R.drawable.mute));
}
else if(mode==AudioManager.RINGER_MODE_VIBRATE) {
myImage.setImageDrawable(getResources() .getDrawable(R.drawable.vibrate));
}
}
});
/* 调整铃声模式为正常模式的Button */
normalButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
/* 设定铃声模式为NORMAL */
audioMa.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
/* 设定音量与声音模式 */
volume=audioMa.getStreamVolume(AudioManager.STREAM_RING);
myProgress.setProgress(volume);
myImage.setImageDrawable(getResources() .getDrawable(R.drawable.normal));
}
});
/* 调整铃声模式为静音模式的Button */
muteButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
/* 设定铃声模式为SILENT
补充:移动开发 , Android ,