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

你可能感兴趣的:(android,手机,private,public,AudioManager)