下面列出了android.media.AudioManager#FLAG_SHOW_UI 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
void setAudioStatus(boolean mute, int volume) {
if (!isTvDeviceEnabled() || !tv().isSystemAudioActivated()) {
return;
}
AudioManager audioManager = getAudioManager();
boolean muted = audioManager.isStreamMute(AudioManager.STREAM_MUSIC);
if (mute) {
if (!muted) {
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
}
} else {
if (muted) {
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
}
// FLAG_HDMI_SYSTEM_AUDIO_VOLUME prevents audio manager from announcing
// volume change notification back to hdmi control service.
int flag = AudioManager.FLAG_HDMI_SYSTEM_AUDIO_VOLUME;
if (0 <= volume && volume <= 100) {
Slog.i(TAG, "volume: " + volume);
flag |= AudioManager.FLAG_SHOW_UI;
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, flag);
}
}
}
private void dispatchVolumeKeyEventLocked(String packageName, int pid, int uid,
boolean asSystemService, KeyEvent keyEvent, int stream, boolean musicOnly) {
boolean down = keyEvent.getAction() == KeyEvent.ACTION_DOWN;
boolean up = keyEvent.getAction() == KeyEvent.ACTION_UP;
int direction = 0;
boolean isMute = false;
switch (keyEvent.getKeyCode()) {
case KeyEvent.KEYCODE_VOLUME_UP:
direction = AudioManager.ADJUST_RAISE;
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
direction = AudioManager.ADJUST_LOWER;
break;
case KeyEvent.KEYCODE_VOLUME_MUTE:
isMute = true;
break;
}
if (down || up) {
int flags = AudioManager.FLAG_FROM_KEY;
if (musicOnly) {
// This flag is used when the screen is off to only affect active media.
flags |= AudioManager.FLAG_ACTIVE_MEDIA_ONLY;
} else {
// These flags are consistent with the home screen
if (up) {
flags |= AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_VIBRATE;
} else {
flags |= AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE;
}
}
if (direction != 0) {
// If this is action up we want to send a beep for non-music events
if (up) {
direction = 0;
}
dispatchAdjustVolumeLocked(packageName, pid, uid, asSystemService, stream,
direction, flags);
} else if (isMute) {
if (down && keyEvent.getRepeatCount() == 0) {
dispatchAdjustVolumeLocked(packageName, pid, uid, asSystemService, stream,
AudioManager.ADJUST_TOGGLE_MUTE, flags);
}
}
}
}