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:
Audiomanager.stream_music/** The audio STREAM for MUSIC playback */
audiomanager.stream_ring/** The audio STREAM for the phone RING */
audiomanager.stream_alarm/** The audio STREAM for alarms */
audiomanager.stream_notification/** The audio STREAM for notifications */
Audiomanager.stream_system/** The audio STREAM for SYSTEM sounds */
Audiomanager.stream_voicecall/** The audio STREAM for phone calls */
Setvolumecontrolstream Function Description:
void android.app.Activity. Setvolumecontrolstream (int streamtype)
suggests an audio stream whose volume should is changed by the hardware volume controls.
The suggested audio stream is tied to the windows of this Activity. If the Activity is a switched, the stream set here is no longer the suggested stream. The client does not need to save and restore the old suggested stream value in OnPause and Onresume.
Parameters:
Streamtype the type of the audio stream whose volume should is changed by the hardware volume controls. It isn't guaranteed that the hardware volume controls would always change this stream's volume (for example, if a call is In progress, the its stream ' s volume is changed instead). To reset back to the default, use Audiomanager.use_default_stream_type.
/////////////////
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 Audiomanager instances:
Java Code Collection Code
1. 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 Collection Code
1. public void Adjuststreamvolume (int streamtype, int direction, int flags)
2.
3. Am.adjuststreamvolume (Audiomanager.stream_music, Audiomanager.adjust_raise, audiomanager.flag_show_ui);
Explain three parameters
Java Code Collection Code
1. The first streamtype is the type of volume that needs to be adjusted, which is set to the media volume, which can be:
2. Stream_alarm Alarm
3. Stream_music Music Playback is the media volume
4. stream_notification window at the top of the status bar NOTIFICATION,
5. Stream_ring Ringtones
6. Stream_system System
7. Stream_voice_call Call
8. STREAM_DTMF dual Tone Multi-frequency, not very understand what things
9.
10. The second direction, which is to adjust the direction, increase or decrease, can be:
One. Adjust_lower Lower Volume
Adjust_raise Raise the Volume
The adjust_same remains the same, which is used primarily to present the current volume to the user.
14.
15. The third flags are additional parameters that describe only two common
Flag_play_sound playing sound when you adjust the volume
FLAG_SHOW_UI the volume bar is displayed when the adjustment is adjusted, the one that appears by pressing the volume key
18.0 means nothing.
2, directly set the volume value method:
Java Code Collection Code
1. public void Setstreamvolume (int streamtype, int index, int flags)
2.
3. Am.setstreamvolume (Audiomanager.stream_music, Am.getstreammaxvolume (Audiomanager.stream_music), Audiomanager.flag_play_sound);
4. Am.getstreammaxvolume (Audiomanager.stream_voice_call);//Get the maximum value of the handset mode
5. Am.getstreamvolume (Audiomanager.stream_voice_call);//Get the current value of the handset mode
The first and third arguments are the same as the previous one
The second parameter is an int value of 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, and I'm going to get to the maximum.
//////////////////////////
The Audiomanager class is located in the Android.media package, which provides access control volume and Qian Sound mode operations.
The official documentation notes: Use Context.getsystemservice (Context.audio_service) to get a instance of this class.
So we can get it through the following statement:
[Java] View plaincopyprint?
1. Audiomanager audiomanage = (Audiomanager) getacitivity (). Getsystemservice (Context.audio_service);
Audiomanager audiomanage = (Audiomanager) getacitivity (). Getsystemservice (Context.audio_service);
Audiomanager is the object that we define to control the sound of the system.
Here are just a few of the more commonly used methods:
Adjustvolume (int direction, int flags)
To control the volume of your phone,
When the first parameter passed in is Audiomanager.adjust_lower, you can turn the volume down one unit,
When you pass in audiomanager.adjust_raise, you can increase the volume by one unit.
Adjuststreamvolume (int streamtype, int direction, intflags)
Adjust phone volume size (in step)
Parameter 1: Sound type, preferably stream_voice_call (call), Stream_system (System sound), stream_ring (ringtone), stream_music (music), Stream_alarm (alarm)
Parameter 2: Adjust the direction of the volume, preferably adjust_lower (lower), adjust_raise (raised), Adjust_same
Parameter 3: Optional flag bit
Setstreamvolume (int streamtype, int index, intflags)
Set the volume size directly
GetMode ()
Returns the current audio mode, such as normal (normal), RINGTONE (ringtone), Orin_call (call)
SetMode ()
Set sound mode, value normal (normal), RINGTONE (ringtone), or in_call (call)
Getringermode ()
Returns the current ringtone mode. such as Ringer_mode_normal (normal), ringer_mode_silent (Mute), ringer_mode_vibrate (vibration)
Setringermode (int ringermode)
Change ringtone mode
Getstreamvolume (int streamtype)
Get the current phone volume, the maximum value is 7, the minimum value is 0, when it is 0 o'clock, the phone automatically adjust the mode to "vibration mode."
Getstreammaxvolume (int streamtype)
Get the maximum ringtone for your current phone.
Setstreammute (int streamtype, Boolean state)
Mute or mute an audio stream
Sets whether the specified sound type (streamtype) is muted. If state is true, it is set to mute, otherwise, it is not set to mute.
[Java] View plaincopyprint?
1.//volume control, initialization definition
2.
3. Audiomanager Maudiomanager = (audiomanager) getsystemservice (Context.audio_service);
4.//MAX Volume
5. int Maxvolume =maudiomanager.getstreammaxvolume (audiomanager.stream_music);
6.//Current Volume
7. int Currentvolume =maudiomanager.getstreamvolume (audiomanager.stream_music);
Volume control, initialization definition
Audiomanager Maudiomanager = (audiomanager) getsystemservice (Context.audio_service);
Maximum volume
int Maxvolume =maudiomanager.getstreammaxvolume (audiomanager.stream_music);
Current volume
int Currentvolume =maudiomanager.getstreamvolume (audiomanager.stream_music);
How much to control the volume directly:
[Java] View plaincopyprint?
1. if (issilent) {
2. Maudiomanager.setstreamvolume (audiomanager.stream_music,0, 0);
3.}else{
4. Maudiomanager.setstreamvolume (audiomanager.stream_music,tempvolume, 0);//tempvolume: Absolute Volume
5.}
if (issilent) {
Maudiomanager.setstreamvolume (audiomanager.stream_music,0, 0);
}else{
Maudiomanager.setstreamvolume (audiomanager.stream_music,tempvolume, 0);//tempvolume: Absolute Volume
}
Control the volume increase or decrease in one step, and eject the system default Volume control bar:
[Java] View plaincopyprint?
1.//Lower the volume, bring up the system volume control
2. if (flag ==0) {
3. Maudiomanager.adjuststreamvolume (Audiomanager.stream_music,audiomanager.adjust_lower,
4. audiomanager.fx_focus_navigation_up);
5.}
6.//Increase the volume, bring up the system volume control
7. Else if (flag ==1) {
8. Maudiomanager.adjuststreamvolume (Audiomanager.stream_music,audiomanager.adjust_raise,
9. audiomanager.fx_focus_navigation_up);
10.}
Lower the volume, bring up the system volume control
if (flag ==0) {
Maudiomanager.adjuststreamvolume (Audiomanager.stream_music,audiomanager.adjust_lower,
AUDIOMANAGER.FX_FOCUS_NAVIGATION_UP);
}
Increase volume, bring up the system volume control
else if (flag ==1) {
Maudiomanager.adjuststreamvolume (Audiomanager.stream_music,audiomanager.adjust_raise,
AUDIOMANAGER.FX_FOCUS_NAVIGATION_UP);
}
Common methods:
Android Audiomanager Get Volume:
Get the Audiomanager instance first,
Audiomanager Maudiomanager = (audiomanager) getsystemservice (Context.audio_service);
[Java] View plaincopyprint?
1.//Call Volume
2.
3. int max = Maudiomanager.getstreammaxvolume (Audiomanager.stream_voice_call);
4.
5. Int current = Maudiomanager.getstreamvolume (Audiomanager.stream_voice_call);
6.
7.//System Volume
8.
9. int max = Maudiomanager.getstreammaxvolume (Audiomanager.stream_system);
10.
One. Current = Maudiomanager.getstreamvolume (Audiomanager.stream_system);
12.
13.//Ringtone Volume
14.
max = Maudiomanager.getstreammaxvolume (audiomanager.stream_ring);
16.
Current = Maudiomanager.getstreamvolume (audiomanager.stream_ring);
18.
19.//Music Volume
20.
max = Maudiomanager.getstreammaxvolume (audiomanager.stream_music);
22.
Current = Maudiomanager.getstreamvolume (Audiomanager.stream_music);
24.
25.
26.
27.//Prompt for sound volume
28.
max = Maudiomanager.getstreammaxvolume (audiomanager.stream_alarm);
30.
Current = Maudiomanager.getstreamvolume (Audiomanager.stream_alarm);
Android Volume Knowledge Summary