1. 程式人生 > >Android通過音量鍵調節音量大小

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);
}

+++參考很多的資料,如有雷同請發郵件給我!^-^+++