SoundPool类下的playSounds方法描写了锁屏的流程:
private void playSounds(boolean locked) {
// User feedback for keyguard.if (mSuppressNextLockSound) {
mSuppressNextLockSound = false;
return;
}
final ContentResolver cr = mContext.getContentResolver();
if (Settings.System.getInt(cr, Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) == 1) {
final int whichSound = locked
? mLockSoundId
: mUnlockSoundId;
mLockSounds.stop(mLockSoundStreamId);
// Init mAudioManager
if (mAudioManager == null) {
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
if (mAudioManager == null) return;
mMasterStreamMaxVolume = mAudioManager.getStreamMaxVolume(MASTER_STREAM_TYPE);
}
// If the stream is muted, don't play the sound
if (mAudioManager.isStreamMute(MASTER_STREAM_TYPE)) return;
// Adjust the lock sound volume from a minimum of MIN_LOCK_VOLUME to a maximum
// of MAX_LOCK_VOLUME, relative to the maximum level of the MASTER_STREAM_TYPE volume.
float lockSoundVolume;
int masterStreamVolume = mAudioManager.getStreamVolume(MASTER_STREAM_TYPE);
if (masterStreamVolume == 0) {
return;
} else {
Xlog.d("ZD","masterStreamVolume="+masterStreamVolume);
Xlog.d("ZD","MIN_LOCK_VOLUME="+MIN_LOCK_VOLUME+"MAX_LOCK_VOLUME"+MAX_LOCK_VOLUME);
Xlog.d("ZD","mMasterStreamMaxVolume="+mMasterStreamMaxVolume);
lockSoundVolume = MIN_LOCK_VOLUME + (MAX_LOCK_VOLUME - MIN_LOCK_VOLUME)
* ((float) masterStreamVolume / mMasterStreamMaxVolume);
Xlog.d("ZD","lock volume="+lockSoundVolume);
}
lockSoundVolume = 1.0f;
mLockSoundStreamId = mLockSounds.play(whichSound, lockSoundVolume, lockSoundVolume, 1,
0, 1.0f);
}
}
masterStreamVolume是系统定义的volume level,play的原型如下:
/**
* Play a sound from a sound ID.
*
* Play the sound specified by the soundID. This is the value
* returned by the load() function. Returns a non-zero streamID
* if successful, zero if it fails. The streamID can be used to
* further control playback. Note that calling play() may cause
* another sound to stop playing if the maximum number of active
* streams is exceeded. A loop value of -1 means loop forever,
* a value of 0 means don't loop, other values indicate the
* number of repeats, e.g. a value of 1 plays the audio twice.
* The playback rate allows the application to vary the playback
* rate (pitch) of the sound. A value of 1.0 means play back at
* the original frequency. A value of 2.0 means play back twice
* as fast, and a value of 0.5 means playback at half speed.
*
* @param soundID a soundID returned by the load() function
* @param leftVolume left volume value (range = 0.0 to 1.0)
* @param rightVolume right volume value (range = 0.0 to 1.0)
* @param priority stream priority (0 = lowest priority)
* @param loop loop mode (0 = no loop, -1 = loop forever)
* @param rate playback rate (1.0 = normal playback, range 0.5 to 2.0)
* @return non-zero streamID if successful, zero if failed
*/
public native final int play(int soundID, float leftVolume, float rightVolume,
int priority, int loop, float rate);