Android Adjusts the sound size 1. Some knowledge of background music
There are a lot of classes on the internet for adding background music:
Mediaplayer,soundpool,audiomanager, but sometimes we can't solve the problems we're having in development.
I'll focus on the experience of adjusting the volume.
1:android Adjust the volume there are many can adjust the volume of the system, the volume of the media, the volume of the alarm, then you need to use the Audiomanager class
首先是得到AudioManager实例:
AudioManager am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
2. Adjust the volume method there are two, one is progressive, that is, like manually press volume keys, step by step increase or decrease, the other is to set the volume value directly.1. The first step is the method:
**public void adjustStreamVolume (int streamType, int direction, int flags); **
am.adjustStreamVolume (AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
-
Explain three parameters
The first streamtype is the type of volume you want to adjust, which is set to the media volume, which can be:
| Field |
Description |
| Stream_alarm |
Alerts |
| Stream_music |
Music playback is media volume |
| Stream_notification |
The status bar at the top of the window notification, |
| Stream_ring |
Ring |
| Stream_system |
System |
| Stream_voice_call |
Call |
| Stream_dtmf |
Dual tone multi-frequency, not very clear what things |
The second direction, which is to adjust the direction, increase or decrease, can be:
| Field |
Description |
| Adjust_lower |
Lower Volume |
| Adjust_raise |
Raise the volume |
| Adjust_same |
This is used primarily to show the user the current volume level |
The third flags are additional parameters that describe only two common
| Field |
Description |
| Flag_play_sound |
Play a sound when you adjust the volume |
| flag_show_ui |
Display the volume bar when adjusting, the one that appears when you press the volume key |
2. Directly set the volume is worth the method:
public void setStreamVolume (int streamType, int index, int flags) { am.setStreamVolume(AudioManager.STREAM_MUSIC, /*修改第二个参数为一个固定的值,就是设置成功。 下面的方法:获取音乐类型的音频流的最大值*/ am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_PLAY_SOUND); }
Android to adjust the sound size