android调节系统音量的实现方式

在Android中,是有几种类型的音量可以调节:

STREAM_ALARM 警报
STREAM_MUSIC 音乐回放即媒体音量
STREAM_RING 铃声
STREAM_SYSTEM 系统
STREAM_VOICE_CALL 通话

代码展示

private void initVolume() {
        //获取系统的Audio管理者
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        //最大音量
        mediaMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        //当前音量
        mediaVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        //seekbar设置最大值为最大音量
        mCarVolume.setMax(mediaMaxVolume);
        //seekbar设置当前进度为当前音量
        setView();
        mCarVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                //设置媒体音量为当前seekbar进度
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
                mediaVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                setView();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private void setView() {
        Log.e("ccm=======>", "mediaVolume: " + mediaVolume);
        mCarVolume.setProgress(mediaVolume);
    }

    /*
     * 事件会在用户按下一个键盘按键时发生
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                if (--mediaVolume >= 0) {
                    setView();
                } else {
                    mediaVolume = 0;
                }
                return true;
            case KeyEvent.KEYCODE_VOLUME_UP:
                if (++mediaVolume <= mediaMaxVolume) {
                    setView();
                } else {
                    mediaVolume = mediaMaxVolume;
                }
                return true;
            case KeyEvent.KEYCODE_VOLUME_MUTE:
                setView();
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }

布局展示




    


工具类

/**
 * 音量工具类
 */
public class VolumeUtil {
    private AudioManager mAudioManager;

    public VolumeUtil(){}

    //获取系统的Audio管理者
    public VolumeUtil(Context context){
        mAudioManager = (AudioManager) context.getSystemService(Service.AUDIO_SERVICE);
    }
    //获取最大多媒体音量
    public int getMediaMaxVolume(){
        Log.e("ccm=======>", "getMediaMaxVolume: " + mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC));
        return mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC );
    }
    //获取当前多媒体音量
    public int getMediaVolume(){
        Log.e("ccm=======>", "getMediaVolume: " +mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC ));
        return mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC );
    }

    //获取最大通话音量
    public int getCallMaxVolume(){
        return mAudioManager.getStreamMaxVolume( AudioManager.STREAM_VOICE_CALL );
    }

    //获取当前通话音量
    public int getCallVolume(){
        return mAudioManager.getStreamVolume( AudioManager.STREAM_VOICE_CALL );
    }

    //获取最大系统音量
    public int getSystemMaxVolume(){

        return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM );
    }

    //获取当前系统音量
    public int getSystemVolume(){

        return mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM );
    }

    //获取最大提示音量
    public int getAlermMaxVolume(){
        return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM );
    }

    //获取当前提示音量
    public int getAlermVolume(){
        return mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM );
    }

    // 设置多媒体音量
    public void setMediaVolume(int volume){
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, //音量类型
                volume,
                AudioManager.FLAG_PLAY_SOUND
                        | AudioManager.FLAG_SHOW_UI);
    }

    //设置通话音量
    public void setCallVolume(int volume){
        mAudioManager.setStreamVolume( AudioManager.STREAM_VOICE_CALL,
                volume,
                AudioManager.FLAG_PLAY_SOUND
                        | AudioManager.FLAG_SHOW_UI);
    }

    //设置提示音量
    public void setAlermVolume(int volume){
        mAudioManager.setStreamVolume( AudioManager.STREAM_ALARM,
                volume,AudioManager.FLAG_PLAY_SOUND
                        |AudioManager.FLAG_SHOW_UI);
    }

    // 关闭/打开扬声器播放
    public void setSpeakerStatus(boolean on) {
        if (on) { //扬声器
            mAudioManager.setSpeakerphoneOn(true);
            mAudioManager.setMode(AudioManager.MODE_NORMAL);
        } else {
            // 设置最大音量
            int max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
            mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, max, AudioManager.STREAM_VOICE_CALL);
            // 设置成听筒模式
            mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
            mAudioManager.setSpeakerphoneOn(false);// 关闭扬声器
            mAudioManager.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
        }
    }

}

你可能感兴趣的:(android调节系统音量的实现方式)