Tian haili @ csdn
2012-04-11
This article describes the ringermode in audio in Android. From the perspective of usage, you only need to pay attention to the ringermode interface in Section 1 audiomanager. The following chapter describes the internal implementation of ringermode in audioservice and how to change the impact of ringermode on an audiostream.
1. Use ringermode with audiomanager
Ringermode is obtained and set through audiomanager. Use context. getsystemservice (context. audio_service) to obtain the local proxy of audioservice, and then call getringermode ()/setringermode () of audiomanager to obtain the current ringermode or set ringermode.
There are three types of ringermode: ringer_mode_silent, ringer_mode_vibrate, and ringer_mode_normal.
After ringermode is setBroadcast ringer_mode_changed_actionAnd the new ringermode will be set inExtra_ringer_modeWhen broadcasting the receiver of ringer_mode_changed_action, you can use intent. getintextra (audiomanager. extra_ringer_mode) to obtain the modified ringermode.
Ii. ringermode internal implementation in audioservice
Audiomanager is only a local proxy. The specific implementation is still in audioservice.
Ringermode in audioserviceMringermodeRecord; stored permanently or in settings. system. The corresponding setting item isMode_ringer, You can use settings. system. getint ()/putint () to operate.
Audiostream described in audio playback in Android: volume and remote playback control is the basis of the androidaudio system. When ringermode is set, the mute operation is mainly involved, while the android audio volume and mute are set according to audiostream.
See the [Implementation of setringermodeint () method for setting ringermode in audioservice]:
mRingerMode = ringerMode;int numStreamTypes = AudioSystem.getNumStreamTypes();for (int streamType = numStreamTypes – 1; streamType >= 0; streamType--) { if (isStreamMutedByRingerMode(streamType)) { if (!isStreamAffectedByRingerMode(streamType) || mRingerMode == AudioManager.RINGER_MODE_NORMAL) { mStreamStates[streamType].mute(null, false); // unmute {streamType} mRingerModeMutedStreams &= ~(1<< streamType); } } else { if (isStreamAffectedByRingerMode(streamType) && mRingerMode !=AudioManager.RINGER_MODE_NORMAL) { mStreamStates[streamType].mute(null, true); // mute {streamType} mRingerModeMutedStreams |=~(1<< streamType); } }}
- Whether changing the ringermode affects the volume (mute) of an audiostream is causedIsstreamaffectedbyringermode(Streamtype: INT) [line #5 & line #10], and this method is used to checkMringermodeaffectedstreamsWhether the bitwise of the audiostream is set, that is,MringermodeaffectedstreamsIndicates whether an audiostream is affected by ringermode.
- If audiostream is mutedMringermodemutedstreams. SoMringermodemutedstreamsIndicates whether audiostream is a bitmasks of all streams that are muted due to ringermode settings.
- Ringermode is recorded inMringermodeBut permanent storage is through settings. System, so after the specific ringermode is setSettings. system. putint(Cr, settings. system.Mode_ringer, Mringermode) to save the current ringermode.
Iii. Impact of ringermode configuration on specific audiostream
As mentioned in the previous section, whether an audiostream is affected by the ringermode setting is causedMringermodeaffectedstreamsIt is determined that, even if the ringermode is set to the mute/vibration mode (ringer_mode_silent/ringer_mode_vibrate), whether or not an audiostream is set to mute or not depends on whether the stream works.MringermodeaffectedstreamsCorresponding to the value of the audiostream bit.
The initial value of mringermodeaffectedstreams in audioservice isSettings. system. getint(Mode_ringer_streams_affected, Defval)
mRingerModeAffectedStreams= Settings.System.getInt(cr,Settings.System.MODE_RINGER_STREAMS_AFFECTED, ((1 << AUdioSystem.STREAM_RING) | (1 << AudioSystem.STREAM_NOTIFICATION) | (1 << AudioSystem.STREAM_SYSTEM)| (1<< AudioSystem.STREAM_SYSTEM_ENFORCED)));
We can see that four audiostreams are affected by the ringermode settings by default.
In this way, you can change the audiostream affected by ringermode settings by setting settings. system. mode_ringer_streams_affected.
For example, alarm's "whether to set an alarm in mute mode" is implemented by setting/removing audiostream_alarm:
Int ringermodestreamtypes = settings. system. getint (Cr, settings. system. mode_ringer_streams_affected, 0); If (Pref. ischecked () {// the alarm is still triggered in the mute mode, that is, alarm is not affected by the ringermode setting ringermodestreamtypes & = ~ (1 <audiomanager. stream_alarm);} else {// no alert in mute mode, that is, alarm is affected by ringermode settings ringermodestreamtypes |=( 1 <audiomanager. stream_alarm);} settings. system. putint (Cr, settings. system. mode_ringer_streams_affected, ringermodestreamtypes );
Once mode_ringer_streams.affected is changed, audioservice will obtain and update the value of mringermodemutedstreams again, and set the ringermode after mringermodemutedstreams is changed through setringermodeint.
In audioservice, you can use settingobserver (inheriting contentobserver) to listen for changes to this setting.
SettingObserver() { mContentResolver.registerContentObserver(Settings.System.getUriFor(Settings.System.MODE_RINGER_STREAMS_AFFECTED), false, this); // ...}@overridepublic void onChange(boolean selfChange) { // ... int ringerModeAffectedStreams = Settings.System.getInt(mContentResolver, Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); if (ringerModeAffectedStreams != mRingerModeAffectedStreams) { mRingerModeAffectedStreams = ringerModeAffectedStreams; setRingerModeInt(getRingerMode(), false); } // ...}
Summary
- Ringermode is centrally managed by the Android system;
- It is configurable to set whether ringermode affects the setting of an audiostream. You can change the audiostream affected by ringermode through configuration;
- The ringermode has changed and can be learned through broadcast.