Android modify system volume and monitor

effect

Insert picture description here

音量The operation of modifying the system is quite common, and it is generally 多媒体involved in development.

Common method

Get audio manager

mAudioManager = this.getSystemService(Context.AUDIO_SERVICE) as AudioManager

Get the maximum media volume

mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)

Get the current media volume of the system

mCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)

Here are a few common ones 音量类型:

  • STREAM_VOICE_CALL call
  • STREAM_SYSTEM system
  • STREAM_RING ringtone
  • STREAM_MUSIC media volume
  • STREAM_ALARM alarm
  • STREAM_NOTIFICATION notification

Get system volume mode

mAudioManager.ringerMode

Volume mode:

  • RINGER_MODE_NORMAL normal
  • RINGER_MODE_SILENT mute
  • RINGER_MODE_VIBRATE vibration

Set system media volume

Let's see how to modify the volume

        btn_add.setOnClickListener {
    
    
            if (mCurrentVolume < mMaxVolume) {
    
    
                mCurrentVolume++
            } else {
    
    
                mCurrentVolume = mMaxVolume
            }
            updateNum(mCurrentVolume)
            setStreamVolume(mCurrentVolume)
        }

        btn_reduce.setOnClickListener {
    
    
            if (mCurrentVolume > 0) {
    
    
                mCurrentVolume--
            } else {
    
    
                mCurrentVolume = 0
            }
            updateNum(mCurrentVolume)
            setStreamVolume(mCurrentVolume)
        }

Note that it is necessary to judge whether the maximum and minimum volume is exceeded.

In the event, in addition to judging the maximum and minimum values, two methods were also called

updateNumThe update page shows:

    /**
     * 更新页面显示
     */
    private fun updateNum(volume: Int) {
    
    
        tv_volume.text = volume.toString()
        seekBar.progress = volume
    }

Also call the setStreamVolumemethod involved here setStreamVolumeand the adjustStreamVolumedifference between:

  • setStreamVolume directly set the volume, refer to which to play
  • adjustStreamVolume Set the volume in steps, ie 10, 20, 30 stepwise

Both can set the volume, you can choose according to your business needs.

setStreamVolume

Take a look at the specific setStreamVolumemethod:

    private fun setStreamVolume(volume: Int) {
    
    
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_SHOW_UI)
    }
  • Parameter 1: Volume type
  • Parameter 2: Volume value
  • Parameter 3:
    • AudioManager.FLAG_SHOW_UI displays the system volume progress bar when adjusting the volume, 0 does not display
    • AudioManager.FLAG_ALLOW_RINGER_MODES is the ringer mode
    • AudioManager.FLAG_VIBRATE is the vibration mode
    • AudioManager.FLAG_SHOW_VIBRATE_HINT vibration prompt
    • AudioManager.FLAG_SHOW_SILENT_HINT Silent prompt
    • AudioManager.FLAG_PLAY_SOUND to play sound when adjusting the volume

adjustStreamVolume

Increasing volume
    private fun adjustRaise() {
    
    
        mAudioManager.adjustStreamVolume(
            AudioManager.STREAM_MUSIC,
            AudioManager.ADJUST_RAISE,
            AudioManager.FLAG_SHOW_UI
        )
    }
  • Parameter 1: Volume type
  • Parameter 2: Volume adjustment direction
    • AudioManager.ADJUST_RAISE The volume gradually increases
    • AudioManager.ADJUST_LOWER The volume gradually decreases
    • AudioManager.ADJUST_SAME unchanged
  • Parameter 3: Same as setStreamVolume parameter 3
Decrease in volume
    private fun adjustLower(volume: Int) {
    
    
        mAudioManager.adjustStreamVolume(
            AudioManager.STREAM_MUSIC,
            AudioManager.ADJUST_LOWER,
            AudioManager.FLAG_SHOW_UI
        )
    }

Github: https://github.com/yechaoa/BrightnessAndVolume

Monitor volume control button

In addition to our manual changes, users can also control the volume through 物理按键or 耳机. At this time, we should also make corresponding changes. Therefore, we need to monitor the volume buttons.

Here you familiar with the old method used in the rewriting Activityof the onKeyDownmethod:

    /**
     * 监听并接管系统的音量按键,
     * 注意:最好保持原有逻辑不变
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
    
    
        when (keyCode) {
    
    
            //音量+按键
            KeyEvent.KEYCODE_VOLUME_UP -> {
    
    
                if (mCurrentVolume < mMaxVolume) {
    
    
                    mCurrentVolume++
                } else {
    
    
                    mCurrentVolume = mMaxVolume
                }
                updateNum(mCurrentVolume)
                setStreamVolume(mCurrentVolume)
                return true
            }
            //音量-按键
            KeyEvent.KEYCODE_VOLUME_DOWN -> {
    
    
                if (mCurrentVolume > 0) {
    
    
                    mCurrentVolume--
                } else {
    
    
                    mCurrentVolume = 0
                }
                updateNum(mCurrentVolume)
                setStreamVolume(mCurrentVolume)
                return true
            }
        }
        return super.onKeyDown(keyCode, event)
    }

It's actually very simple, intercept the event, and then execute our logic.

to sum up

In general, the amount of code is not large, and the degree of difficulty is not high. The only thing to pay attention to is the type of each parameter, which can be selected according to your actual business.

ok, so far 修改音量the explanation about it is all over, if it is useful to you, just like it ^-^

Github

https://github.com/yechaoa/BrightnessAndVolume

Guess you like

Origin blog.csdn.net/yechaoa/article/details/112248013