Android通过音量键调节音量大小

通过程序实现对android系统手机音量的调节

1、获得系统服务

AudioManager(音频管理器)了,该类位于Android.Media包下,提供了音量控制与铃声模式相关操作!使用Context.getSystemService(Context.AUDIO.SERVICE)获得类的实例

AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

2、获得音量按键的响应


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_DOWN:
	//对应音量-操作
            return true;

        case KeyEvent.KEYCODE_VOLUME_UP:
	//对应音量+操作
            return true;

        default:
            break;
    }
    return super.onKeyDown(keyCode, event);
}

3、音量控制

----------------------------------

1、渐进式控制

public void adjustStreamVolume(int streamType, int direction, int flags) {...}

参数1:streamType需要调整音量的类型,这里设的是媒体音量。有如下类型

/** The audio stream for phone calls */
public static final int STREAM_VOICE_CALL = AudioSystem.STREAM_VOICE_CALL;
/** The audio stream for system sounds */
public static final int STREAM_SYSTEM = AudioSystem.STREAM_SYSTEM;
/** The audio stream for the phone ring */
public static final int STREAM_RING = AudioSystem.STREAM_RING;
/** The audio stream for music playback */
public static final int STREAM_MUSIC = AudioSystem.STREAM_MUSIC;
/** The audio stream for alarms */
public static final int STREAM_ALARM = AudioSystem.STREAM_ALARM;
/** The audio stream for notifications */
public static final int STREAM_NOTIFICATION = AudioSystem.STREAM_NOTIFICATION;
/** @hide The audio stream for phone calls when connected to bluetooth */
public static final int STREAM_BLUETOOTH_SCO = AudioSystem.STREAM_BLUETOOTH_SCO;
/** @hide The audio stream for enforced system sounds in certain countries (e.g camera in Japan) */
public static final int STREAM_SYSTEM_ENFORCED = AudioSystem.STREAM_SYSTEM_ENFORCED;
/** The audio stream for DTMF Tones */
public static final int STREAM_DTMF = AudioSystem.STREAM_DTMF;
/** @hide The audio stream for text to speech (TTS) */
public static final int STREAM_TTS = AudioSystem.STREAM_TTS;

参数2:direction是调整的方向,增加或减少。

     AudioManager.ADJUST_RAISE,

    AudioManager.ADJUST_LOWER,

参数3:flags是一些附加参数,只介绍两个常用的

public static final int FLAG_PLAY_SOUND = 1 << 2; //调整音量时播放声音
 
  
public static final int FLAG_SHOW_UI = 1 << 0;	//调整音量是显示UI

    mAudioManager.adjustStreamVolume(
            AudioManager.STREAM_MUSIC,
            AudioManager.ADJUST_RAISE,
            AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);

--------------------------------------------

2、直接设置音量

获取音量的最大值和当前值,通过算法设置响应的音量

public void setStreamVolume(int streamType, int index, int flags) {
 
  参数1和参数3的等同于上一节中的参数,参数2就是你想设置的音量值 
  


int max,current;
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC );	//获得最大值
current = mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC );	//获得当前值

4、对应代码

//渐进式
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            mAudioManager.adjustStreamVolume(
                    AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_LOWER,
                    AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
            return true;

        case KeyEvent.KEYCODE_VOLUME_UP:
            mAudioManager.adjustStreamVolume(
                    AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_RAISE,
                    AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
            return true;

        default:
            break;
    }
    return super.onKeyDown(keyCode, event);
}
//直接设置
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    int max,current,volume;

    AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC );
    volume = mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC );

    switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            if(volume < max)
                volume++;

            mAudioManager.setStreamVolume (
                    AudioManager.STREAM_MUSIC,
                    volume,
                    AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
            return true;

        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if(volume > 0)
                volume--;

            mAudioManager.setStreamVolume (
                    AudioManager.STREAM_MUSIC,
                    volume,
                    AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
            return true;

        default:
            break;
    }
    return super.onKeyDown(keyCode, event);
}

+++参考很多的资料,如有雷同请发邮件给我!^-^+++

你可能感兴趣的:(apk开发)