-
Notifications
You must be signed in to change notification settings - Fork 14
/
AudioUtils.kt
55 lines (46 loc) · 1.54 KB
/
AudioUtils.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.excellence.basetoolslibrary.utils
import android.content.Context
import android.media.AudioManager
import android.os.Build
/**
* <pre>
* author : VeiZhang
* blog : http://tiimor.cn
* time : 2022/4/21
* desc :
* </pre>
*/
object AudioUtils {
@JvmStatic
fun getAudioManager(context: Context): AudioManager {
return context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
}
@JvmStatic
fun getMaxVolume(context: Context, streamType: Int): Int {
return getAudioManager(context).getStreamMaxVolume(streamType)
}
@JvmStatic
fun getMinVolume(context: Context, streamType: Int): Int {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getAudioManager(context).getStreamMinVolume(streamType)
} else {
0
}
}
@JvmStatic
fun getVolume(context: Context, streamType: Int): Int {
return getAudioManager(context).getStreamVolume(streamType)
}
@JvmStatic
fun setVolume(context: Context, streamType: Int, volume: Int) {
getAudioManager(context).setStreamVolume(streamType, volume, AudioManager.FLAG_PLAY_SOUND)
}
@JvmStatic
fun adjustStreamVolume(context: Context, streamType: Int, direction: Int) {
getAudioManager(context).adjustStreamVolume(streamType, direction, AudioManager.FLAG_PLAY_SOUND)
}
@JvmStatic
fun adjustVolume(context: Context, direction: Int) {
getAudioManager(context).adjustVolume(direction, AudioManager.FLAG_PLAY_SOUND)
}
}