当开发多媒体应用或者游戏应用的时候,需要使用音量控制键来设置程序的音量大小。在Android系统中有多种音频流,通过Activity中的函数 setVolumeControlStream(int streamType)可以设置该Activity中音量控制键控制的音频流,一般在onCreate函数中设置。
Android中有如下几种音频流:
- AudioManager.STREAM_MUSIC /** The audio stream for music playback */
- AudioManager.STREAM_RING /** The audio stream for the phone ring */
- AudioManager.STREAM_ALARM /** The audio stream for alarms */
- AudioManager.STREAM_NOTIFICATION /** The audio stream for notifications */
- AudioManager.STREAM_SYSTEM /** The audio stream for system sounds */
- AudioManager.STREAM_VOICECALL /** The audio stream for phone calls */
及其自带调节音量的方法:
// 调高音量
AudioManager .adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
// 调低音量
AudioManager .adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);