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
推荐阅读
-
Android Studio 基础之获取Wifi 的状态,设置 Wifi 的开关状态,并监听 Wifi 的状态变化方法整理
-
Android开发之全屏与非全屏的切换设置方法小结
-
Android开发之全屏与非全屏的切换设置方法小结
-
android编程获取和设置系统铃声和音量大小的方法
-
Android开发学习之WallPaper设置壁纸详细介绍与实例
-
Android开发之获取单选与复选框的值操作示例
-
android编程获取和设置系统铃声和音量大小的方法
-
Android开发学习之WallPaper设置壁纸详细介绍与实例
-
Android adb命令设置和获取系统音量
-
Android Studio 基础 之 获取蓝牙Bluetooth 的状态,设置的蓝牙Bluetooth 的开关状态,并监听蓝牙Bluetooth 的状态变化方法整理