2023-01-31 21:13:39 +05:30
|
|
|
package com.github.libretube.helpers
|
2022-11-25 16:26:45 +05:30
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.media.AudioManager
|
2023-02-10 04:52:31 +05:30
|
|
|
import androidx.core.content.getSystemService
|
2022-11-25 16:26:45 +05:30
|
|
|
import androidx.core.math.MathUtils
|
2023-02-10 04:52:31 +05:30
|
|
|
import androidx.media.AudioManagerCompat
|
2022-11-25 16:26:45 +05:30
|
|
|
import com.github.libretube.extensions.normalize
|
|
|
|
|
2023-02-10 04:52:31 +05:30
|
|
|
class AudioHelper(context: Context) {
|
|
|
|
private val audioManager = context.getSystemService<AudioManager>()!!
|
|
|
|
private val minimumVolumeIndex = AudioManagerCompat
|
|
|
|
.getStreamMinVolume(audioManager, AudioManager.STREAM_MUSIC)
|
|
|
|
private val maximumVolumeIndex = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
|
2022-11-25 16:26:45 +05:30
|
|
|
|
|
|
|
var volume: Int
|
2023-02-10 04:52:31 +05:30
|
|
|
get() = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) - minimumVolumeIndex
|
2022-11-25 16:26:45 +05:30
|
|
|
set(value) {
|
2023-02-10 04:52:31 +05:30
|
|
|
val vol = MathUtils.clamp(value, minimumVolumeIndex, maximumVolumeIndex)
|
|
|
|
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, vol, 0)
|
2022-11-25 16:26:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
fun setVolumeWithScale(value: Int, maxValue: Int, minValue: Int = 0) {
|
|
|
|
volume = value.normalize(minValue, maxValue, minimumVolumeIndex, maximumVolumeIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getVolumeWithScale(maxValue: Int, minValue: Int = 0): Int {
|
|
|
|
return volume.normalize(minimumVolumeIndex, maximumVolumeIndex, minValue, maxValue)
|
|
|
|
}
|
|
|
|
}
|