欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android实战之音量获取与设置

程序员文章站 2022-05-11 16:57:30
...

获取和设置安卓音量信息主要使用了到AudioManager,通过getSystemService拿到AudioManager,然后就可以使用其提供的API轻松完成音量值的获取和设置了。

获取各类别音量最大值与当前值

    fun getVoiceInfo(view: View) {
        val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
        // 通话音量
        val callVoiceMax = audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL)
        val callVoiceCurrent = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL)
        println("callVoiceMax:$callVoiceMax ,callVoiceCurrent:$callVoiceCurrent")
        // 系统音量
        val systemVoiceMax = audioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM)
        val systemVoiceCurrent = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM)
        println("systemVoiceMax:$systemVoiceMax ,systemVoiceCurrent:$systemVoiceCurrent")
        // 铃声音量
        val ringVoiceMax = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING)
        val ringVoiceCurrent = audioManager.getStreamVolume(AudioManager.STREAM_RING)
        println("ringVoiceMax:$ringVoiceMax ,ringVoiceCurrent:$ringVoiceCurrent")
        // 音乐音量
        val musicVoiceMax = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
        val musicVoiceCurrent = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
        println("musicVoiceMax:$musicVoiceMax ,musicVoiceCurrent:$musicVoiceCurrent")
        // 提示音音量
        val alarmVoiceMax = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM)
        val alarmVoiceCurrent = audioManager.getStreamVolume(AudioManager.STREAM_ALARM)
        println("alarmVoiceMax:$alarmVoiceMax ,alarmVoiceCurrent:$alarmVoiceCurrent")
    }

输出为:


10-22 14:18:48.810 24532-24532/com.happy.deviceinfo.demo I/System.out: callVoiceMax:11 ,callVoiceCurrent:5

10-22 14:18:48.812 24532-24532/com.happy.deviceinfo.demo I/System.out: systemVoiceMax:15 ,systemVoiceCurrent:6

10-22 14:18:48.813 24532-24532/com.happy.deviceinfo.demo I/System.out: ringVoiceMax:15 ,ringVoiceCurrent:6

10-22 14:18:48.819 24532-24532/com.happy.deviceinfo.demo I/System.out: musicVoiceMax:15 ,musicVoiceCurrent:2

10-22 14:18:48.821 24532-24532/com.happy.deviceinfo.demo I/System.out: alarmVoiceMax:15 ,alarmVoiceCurrent:10

设置音量值

同理,通过AudioManager提供的setStreamVolume可轻松完成设置,以音乐音量为例:

fun setMusicVoice(view: View) {
        val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
        // 设置前音量
        val musicVoiceCurrentBeforeSet = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
        println("设置前:musicVoiceCurrentBeforeSet:$musicVoiceCurrentBeforeSet")
        // 设置音量
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_PLAY_SOUND)
        // 设置好音量
        val musicVoiceCurrentAfterSet = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
        println("设置前:musicVoiceCurrentAfterSet:$musicVoiceCurrentAfterSet")
    }

输出为:

10-22 14:37:30.816 25012-25012/com.happy.deviceinfo.demo I/System.out: 设置前:musicVoiceCurrentBeforeSet:2

10-22 14:37:30.827 25012-25012/com.happy.deviceinfo.demo I/System.out: 设置前:musicVoiceCurrentAfterSet:0