When developing multimedia applications or gaming applications, you need to use the volume control keys to set the volume of your program. There is a multi-medium audio stream in the Android system, and the function setvolumecontrolstream (int streamtype) in the activity allows you to set the audio stream controlled by the volume Control key in the activity. Typically set in the OnCreate function. There are several audio streams in Android (Streamtype is the type that you need to adjust the volume):
Audiomanager.stream_music/Music playback is the media volume/
audiomanager.stream_ring/Ringtones/
Audiomanager.stream_alarm/Alarm/
Audiomanager.stream_notification/window top status bar notification sound/
Audiomanager.stream_system/System/
Audiomanager.stream_voicecall/Call/
AUDIOMANAGER.STREAM_DTMF/Dual tone multi-frequency, not very understand what things/
Audiomanager can modify the volume of the system's Android system, the following describes several Audiomanager methods of volume adjustment. The first is to get the Audiomanager instance:
Java code
Audiomanager am= (Audiomanager) Getsystemservice (Context.audio_service);
There are two ways to adjust the volume method, one is progressive, that is, like manually pressing the volume key, step by step increase or decrease, the other is to set the volume value directly. 1. Progressive type
Java code
Publicvoidadjuststreamvolume (Intstreamtype, intdirection, intflags)
Am.adjuststreamvolume (Audiomanager.stream_music, Audiomanager.adjust_raise, audiomanager.flag_show_ui);
Explain three parameters
Java code
The first streamtype is the type of volume you want to adjust, which is set to the media volume, which can be:
Stream_alarm Alarm
Stream_music Music playback is media volume
Stream_notification window at the top of the status bar NOTIFICATION,
Stream_ring Ringtones
Stream_system system
Stream_voice_call Call
STREAM_DTMF dual tone Multi-frequency, not very understand what things
The second direction, which is to adjust the direction, increase or decrease, can be:
Adjust_lower lowering the volume
Adjust_raise Raise the Volume
The adjust_same remains the same, which is used primarily to show the current volume to the user
The third flags are additional parameters that describe only two common
Flag_play_sound playing 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
0 means there's nothing.
2, directly set the volume value method:
Java code
Publicvoidsetstreamvolume (Intstreamtype, Intindex, intflags)
Am.setstreamvolume (Audiomanager.stream_music, Am.getstreammaxvolume (Audiomanager.stream_music), Audiomanager.flag_play_sound);
Am.getstreammaxvolume (Audiomanager.stream_voice_call);//Get the maximum value of the handset mode
Am.getstreamvolume (Audiomanager.stream_voice_call);//Get the current value of the handset mode
The first and third parameters are the same as the second argument above is the int value of the volume, getstreammaxvolume (int streamtype) is the maximum value of the volume of that type, you can calculate the volume you need based on this value, I am here to adjust the maximum.
Android Volume Control Setvolumecontrolstream