Android volume adjustment implementation (ringtonemanager and ringervolumepreference ).

Source: Internet
Author: User

 

Address: http://blog.csdn.net/liranke/article/details/6683000

Android volume adjustment is implemented in two ways. One is to adjust the volume on the settings page by pressing the side key; the other is to adjust the volume on a non-settings page, press the side key to adjust the volume. The two adjustment methods have different implementations. They operate two different volume seekbar. The following is an analysis:

1. On the volume setting page of settings, press the side key to adjust the volume:

On the volume setting page of settings, press the side key to adjust the volume and play the music (this music is set in the ringtone setting ), the code for implementing the entire process is as follows:
1. Volume setting page of settings:
File: packages/apps/settings/src/COM/Android/settings/ringervolumepreference. Java
Note: In this ringervolumepreference, audiomanager. setstreamvolume is called to adjust the volume. However, the playback sound is not in this file.
2. Play Music:
File: Frameworks/base/CORE/Java/Android/preference/volumepreference. Java
Note: ringervolumepreference is inherited from volumepreference. When you click a side key, the onkey function of volumepreference receives the key value and plays the music using mseekbarvolumizer. changevolumeby, sample, and mringtone. Play.

It is worth noting that mringtone. Play uses the new mediaplayer method to play music by creating a mediaplayer.

2. Adjust the volume by pressing the side key on the non-settings page:

Implementation: The ringtonemanager will call the adjuststreamvolume of audiomanager, and then call the adjuststreamvolume of audioservicer. The most important thing is this function.

In this function:

1. checkforringermodechange (oldindex, direction); oldindex indicates the previous volume, and direction indicates whether to increase or decrease the volume. This function is used to detect and change the ringtone mode, such as vibration and mute, by calling the setringermode function. Setringermode notifies the mode change by sending a broadcast (calling the broadcastringermode function.

2. sendmsg: Send a message. The handlemessage function is used to process the message and call setstreamvolumeindex to adjust the volume. In fact, it is implemented through the corresponding function of audiopolicyservice.

3. mvolumepanel. postvolumechanged (streamtype, flags); // used to update the UI.

4. sendvolumeupdate: The audiomanager. volume_changed_action will be sent. The intent is to play the transient beep sound (see the starttone of tonegenerator. Java ).

5. Analysis of tonegenerator. Java starttone: Call the starttone function of tonegenerator. cpp through JNI.

It is worth noting that in the end, audiotrack is used to process this sound, rather than creating mediaplay, which is different from the processing method in settings.

Digression: The call sound playback method is also implemented through ringtone and mediaplay is created.

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.