android 控制手机音量的大小 切换声音的模式
2014-05-09 14:15
489 查看
(1)程序说明
在android API的AudioManager中,提供了调节手机音量的办法。
audioMa.adjustVolume(AudioManager.ADJUST_LOWER, 0);
audioMa.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioMa.setRingerMode(AudioManager.RINGER_MODE_SILENT);
audioMa.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
(2)布局文件
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/myText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_text1"
android:textSize="16sp"
android:textColor="@drawable/black"
android:layout_x="20px"
android:layout_y="42px"
>
</TextView>
<ImageView
android:id="@+id/myImage"
android:layout_width="48px"
android:layout_height="48px"
android:layout_x="110px"
android:layout_y="32px"
>
</ImageView>
<TextView
android:id="@+id/myText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_text2"
android:textSize="16sp"
android:textColor="@drawable/black"
android:layout_x="20px"
android:layout_y="102px"
>
</TextView>
<ProgressBar
android:id="@+id/myProgress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="160dip"
android:layout_height="wrap_content"
android:max="7"
android:progress="5"
android:layout_x="110px"
android:layout_y="102px"
>
</ProgressBar>
<ImageButton
android:id="@+id/downButton"
android:layout_width="100px"
android:layout_height="100px"
android:layout_x="50px"
android:layout_y="162px"
android:src="@drawable/down"
>
</ImageButton>
<ImageButton
android:id="@+id/upButton"
android:layout_width="100px"
android:layout_height="100px"
android:layout_x="150px"
android:layout_y="162px"
android:src="@drawable/up"
>
</ImageButton>
<ImageButton
android:id="@+id/normalButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="50px"
android:layout_y="272px"
android:src="@drawable/normal"
>
</ImageButton>
<ImageButton
android:id="@+id/muteButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="120px"
android:layout_y="272px"
android:src="@drawable/mute"
>
</ImageButton>
<ImageButton
android:id="@+id/vibrateButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="190px"
android:layout_y="272px"
android:src="@drawable/vibrate"
>
</ImageButton>
</AbsoluteLayout>
(3)代码:
package com.liuzuyi.soundmode;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class MainActivity 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;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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));
}
downbutton.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
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));
}
}
});
upbutton.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
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));
}
}
});
normalbutton.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
audioMa.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);
myprogress.setProgress(volume);
myimage.setImageDrawable(getResources().getDrawable(R.drawable.normal));
}
});
mutebutton.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
audioMa.setRingerMode(AudioManager.RINGER_MODE_SILENT);
volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);
myprogress.setProgress(volume);
myimage.setImageDrawable(getResources().getDrawable(R.drawable.mute));
}
});
vibratebutton.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
audioMa.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);
myprogress.setProgress(volume);
myimage.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));
}
});
}
}
在android API的AudioManager中,提供了调节手机音量的办法。
audioMa.adjustVolume(AudioManager.ADJUST_LOWER, 0);
audioMa.adjustVolume(AudioManager.ADJUST_RAISE, 0);也可以调节手机声音的模式为震动或者静音
audioMa.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioMa.setRingerMode(AudioManager.RINGER_MODE_SILENT);
audioMa.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
(2)布局文件
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/myText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_text1"
android:textSize="16sp"
android:textColor="@drawable/black"
android:layout_x="20px"
android:layout_y="42px"
>
</TextView>
<ImageView
android:id="@+id/myImage"
android:layout_width="48px"
android:layout_height="48px"
android:layout_x="110px"
android:layout_y="32px"
>
</ImageView>
<TextView
android:id="@+id/myText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_text2"
android:textSize="16sp"
android:textColor="@drawable/black"
android:layout_x="20px"
android:layout_y="102px"
>
</TextView>
<ProgressBar
android:id="@+id/myProgress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="160dip"
android:layout_height="wrap_content"
android:max="7"
android:progress="5"
android:layout_x="110px"
android:layout_y="102px"
>
</ProgressBar>
<ImageButton
android:id="@+id/downButton"
android:layout_width="100px"
android:layout_height="100px"
android:layout_x="50px"
android:layout_y="162px"
android:src="@drawable/down"
>
</ImageButton>
<ImageButton
android:id="@+id/upButton"
android:layout_width="100px"
android:layout_height="100px"
android:layout_x="150px"
android:layout_y="162px"
android:src="@drawable/up"
>
</ImageButton>
<ImageButton
android:id="@+id/normalButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="50px"
android:layout_y="272px"
android:src="@drawable/normal"
>
</ImageButton>
<ImageButton
android:id="@+id/muteButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="120px"
android:layout_y="272px"
android:src="@drawable/mute"
>
</ImageButton>
<ImageButton
android:id="@+id/vibrateButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="190px"
android:layout_y="272px"
android:src="@drawable/vibrate"
>
</ImageButton>
</AbsoluteLayout>
(3)代码:
package com.liuzuyi.soundmode;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class MainActivity 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;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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));
}
downbutton.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
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));
}
}
});
upbutton.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
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));
}
}
});
normalbutton.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
audioMa.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);
myprogress.setProgress(volume);
myimage.setImageDrawable(getResources().getDrawable(R.drawable.normal));
}
});
mutebutton.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
audioMa.setRingerMode(AudioManager.RINGER_MODE_SILENT);
volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);
myprogress.setProgress(volume);
myimage.setImageDrawable(getResources().getDrawable(R.drawable.mute));
}
});
vibratebutton.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
audioMa.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);
myprogress.setProgress(volume);
myimage.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));
}
});
}
}
相关文章推荐
- android 控制手机的体积的大小 切换音效模式
- 简单的Android音乐播放,解决incall类型不能通过音量按键控制声音大小的问题
- Android通过摇晃手机的频率来控制声音的频率
- Android通过摇晃手机的频率来控制声音的频率
- ios音量获取与手机静音模式下播放声音
- android控制手机系统声音
- android静音模式下仍可以播放媒体声音以及调节其大小。。。。
- Android通过摇晃手机的频率来控制声音的频率
- Android声音大小控制——AudioManager使用
- 【Unity快速实现小功能】声音控制三部曲之(三)—— 声音、音效开关以及音量大小控制
- Android手机音量的控制
- 手机音量键控制SoundPool大小
- android手机连接PC后怎样切换为U 盘模式
- cocos2d-x 控制android硬件音量大小调节按钮
- android 手机听筒和扬声器切换 听筒没声音问题
- 如何调整Android手机的开机音量大小
- android应用内部实现生成桌面快捷方式与进度条控制音量大小
- 关于Android手机声音音量的获取和设置
- android控制手机系统声音
- Android编程获取手机屏幕分辨率大小的方法