Android音量調節AudioManager
使用AudioManager管理手機音量。
簡單小例子:
四個按鈕:靜音,音量+,音量—,最大音量。
MainActivity.java代碼:
public class MainActivity extends Activity {
private AudioManager audiomanager;
private Button button1,button2,button3,button4;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
button4=(Button)findViewById(R.id.button4);
audiomanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
audiomanager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
audiomanager.adjustVolume(AudioManager.ADJUST_RAISE, 0);
}
});
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
audiomanager.adjustVolume(AudioManager.ADJUST_LOWER, 0);
}
});
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
int maxvolume=audiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
audiomanager.setStreamVolume(AudioManager.STREAM_MUSIC,maxvolume , 0);
}
});
}
}
其中,根部不同情況進行不同音量調節。
1.通話音量
AudioManager.STREAM_VOICE_CALL
2.系統音量
AudioManager.STREAM_SYSTEM
3.鈴聲音量
AudioManager.STREAM_RING
4.音樂音量
AudioManager.STREAM_MUSIC
5.提示聲音音量
AudioManager.STREAM_ALARM