mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-16 15:20:31 +05:30
31 lines
1.2 KiB
Kotlin
31 lines
1.2 KiB
Kotlin
package com.github.libretube.helpers
|
|
|
|
import android.content.Context
|
|
import android.media.AudioManager
|
|
import androidx.core.content.getSystemService
|
|
import androidx.core.math.MathUtils
|
|
import androidx.media.AudioManagerCompat
|
|
import com.github.libretube.extensions.normalize
|
|
|
|
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)
|
|
|
|
var volume: Int
|
|
get() = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) - minimumVolumeIndex
|
|
set(value) {
|
|
val vol = MathUtils.clamp(value, minimumVolumeIndex, maximumVolumeIndex)
|
|
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, vol, 0)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|