Android Volume adjustment

Source: Internet
Author: User

For Android volume adjustment, the volume can be divided into key adjust volume and settings. Let's start by talking about the volume adjustment in the settings.

First, the volume of the classification:

1.audiomanager.stream_voice_call

2.audiomanager.stream_ring

3.audiomanager.stream_music,

4.audiomanager.stream_alarm

5.audiomanager.stream_notification

Second, the volume range:

For different types of volume Android specifies a different range, in Audioservice there is an array that defines the range of different volume levels.

Private final int[] Max_stream_volume = new int[] {
5,//Stream_voice_call
7,//Stream_system
7,//stream_ring
,//Stream_music
7,//Stream_alarm
7,//Stream_notification
,//Stream_bluetooth_sco
7,//stream_system_enforced
,//STREAM_DTMF
//Stream_tts
};

Three: How to adjust the volume:

int streamvalue = Am.getstreamvolume (Streamtype); Gets the current type of volume value

Audiomanager Audiomanager = (audiomanager) getsystemservice (Context.audio_service); Get Audio Service

Audiomanager.setstreamvolume (Mstreamtype, Streamvalue, 0); Set volume

Now, let's talk. Use the keys to adjust the volume

Use the key to adjust the volume, first in the Phonewinmanager receive a key event, and then call Audioservice in the Adjuststreamvolume method, the source code is as follows:

/**
* Tell the Audio service to adjust the volume appropriate to the event.
* @param keycode
*/
void ha Ndlevolumekey (int stream, int keycode) {
Iaudioservice audioservice = Getaudioservice ();
if (Audioservice = = null) {
return;
}
Try {
//Since audio is playing, we shouldn ' t has a to-hold a wake lock
//during the "call", but we do it As a precaution for the rare possibility
//So the music stops right before we ' this
//todo:actually Hand Le MUTE.
Mbroadcastwakelock.acquire ();
Audioservice.adjuststreamvolume (stream,
KeyCode = = Keyevent.keycode_volume_up
? Audiomanager.adjust_raise
: Audiomanager.adjust_lower,
0),
} catch (RemoteException e) {
LOG.W (TAG, " Iaudioservice.adjuststreamvolume () threw remoteexception "+ e);
} finally {
Mbroadcastwakelock.release ();
}
}

The Adjuststreamvolume will start the Volumepanel, which is the interface we press the volume key to appear. The Audiomanager Setstreamvolume is called in Volumepanel to set the volume.

Android Volume adjustment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.