获取系统音量
通过程序获取android系统手机的铃声和音量。同样,设置铃声和音量的方法也很简单!
设置音量的方法也很简单,AudioManager提供了方法:
public void setStreamVolume(intstreamType,intindex,intflags)
其中streamType有内置的常量:
AudioManager.STREAM_MUSIC
AudioManager.STREAM_VOICE_CALL
AudioManager.STREAM_SYSTEM
AudioManager.STREAM_RING
intflags也是内置常量:
FLAG_ALLOW_RINGER_MODES,FLAG_PLAY_SOUND,FLAG_REMOVE_SOUND_AND_VIBRATE,FLAG_SHOW_UI,FLAG_VIBRATE
如进入一个页面希望设置媒体音量为最大:代码如下:
//获取最大媒体音量值
int max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//设置媒体音量为最大值,当然也可以设置媒体音量为其他给定的值
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, max,0);
JAVA代码:
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//通话音量
int max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_VOICE_CALL );
int current = mAudioManager.getStreamVolume( AudioManager.STREAM_VOICE_CALL );
Log.d(“VIOCE_CALL”, “max : ” + max + ” current : ” + current);
//系统音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_SYSTEM );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_SYSTEM );
Log.d(“SYSTEM”, “max : ” + max + ” current : ” + current);
//铃声音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_RING );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_RING );
Log.d(“RING”, “max : ” + max + ” current : ” + current);
//音乐音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC );
Log.d(“MUSIC”, “max : ” + max + ” current : ” + current);
//提示声音音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_ALARM );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_ALARM );
Log.d(“ALARM”, “max : ” + max + ” current : ” + current);
ps:
游戏过程中只允许调整多媒体音量,而不允许调整通话音量。
setVolumeControlStream(AudioManager.STREAM_MUSIC);
长时间不动,不允许黑屏,View.setKeepScreenOn(true);
估计manifest文件中需要注册权限吧
调节媒体音量
AudioManager audio = (AudioManager) getSystemService(Service.AUDIO_SERVICE);
//参数三AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI表示在调整媒体音量的时候会发出声音,并且弹出音量调整对话框
//如果不想要这些,可以设置为0
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND
| AudioManager.FLAG_SHOW_UI);
重写 Activity 的 onKeyDown 方法
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
audio.adjustStreamVolume(
AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE,
AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
audio.adjustStreamVolume(
AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
return true;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
原文链接:https://blog.csdn.net/daiqiquan/java/article/details/41347751