package
com.example.newcanaryproject
import
android.content.Context
import
android.content.Context.AUDIO_SERVICE
import
android.media.AudioManager
import
android.os.Bundle
import
android.util.Log
import
androidx.activity.ComponentActivity
import
androidx.activity.compose.setContent
import
androidx.compose.foundation.BorderStroke
import
androidx.compose.foundation.layout.*
import
androidx.compose.foundation.shape.RoundedCornerShape
import
androidx.compose.material.*
import
androidx.compose.runtime.Composable
import
androidx.compose.runtime.mutableStateOf
import
androidx.compose.runtime.remember
import
androidx.compose.ui.Alignment
import
androidx.compose.ui.Modifier
import
androidx.compose.ui.draw.clip
import
androidx.compose.ui.graphics.Color
import
androidx.compose.ui.graphics.SolidColor
import
androidx.compose.ui.platform.LocalContext
import
androidx.compose.ui.text.font.FontWeight
import
androidx.compose.ui.text.style.TextAlign
import
androidx.compose.ui.unit.Dp
import
androidx.compose.ui.unit.dp
import
androidx.compose.ui.unit.sp
import
com.example.newcanaryproject.ui.theme.NewCanaryProjectTheme
import
com.example.newcanaryproject.ui.theme.greenColor
import
com.github.skydoves.colorpicker.compose.*
class
MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super
.onCreate(savedInstanceState)
setContent {
NewCanaryProjectTheme {
Surface(
modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
) {
Scaffold(
topBar = {
TopAppBar(backgroundColor = greenColor,
title = {
Text(
text =
"GFG"
,
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Center,
color = Color.White
)
})
}) {
volumeController(LocalContext.current)
}
}
}
}
}
}
@Composable
fun volumeController(context: Context) {
val audioManager = context.getSystemService(AUDIO_SERVICE) as AudioManager
val volumeLevel = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
val maxVolumeLevel = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
val volumePercent = (volumeLevel.toFloat() / maxVolumeLevel *
100
).toInt()
val currentVolume = remember {
mutableStateOf(volumePercent)
}
Column(
modifier = Modifier
.fillMaxSize()
.padding(all =
30
.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
Text(
text =
"Volume Controller"
,
fontWeight = FontWeight.Bold,
fontSize =
20
.sp,
color = greenColor,
)
Text(
text =
"Volume Level : "
+ currentVolume.value,
fontWeight = FontWeight.Bold,
fontSize =
20
.sp,
color = greenColor
)
Row(modifier = Modifier.padding(
4
.dp)) {
Button(
onClick = {
audioManager.adjustVolume(
AudioManager.ADJUST_RAISE,
AudioManager.FLAG_PLAY_SOUND
)
val volumeLevel = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
val maxVolumeLevel = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
val volumePercent = (volumeLevel.toFloat() / maxVolumeLevel *
100
).toInt()
currentVolume.value = volumePercent
},
modifier = Modifier.padding(all = Dp(10F)),
enabled =
true
,
border = BorderStroke(width =
1
.dp, brush = SolidColor(Color.Blue)),
shape = MaterialTheme.shapes.medium,
colors = ButtonDefaults.buttonColors(backgroundColor = greenColor),
) {
Text(text =
"+"
)
}
Spacer(modifier = Modifier.width(
20
.dp))
Button(
onClick = {
audioManager.adjustVolume(
AudioManager.ADJUST_LOWER,
AudioManager.FLAG_PLAY_SOUND
)
val volumeLevel = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
val maxVolumeLevel = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
val volumePercent = (volumeLevel.toFloat() / maxVolumeLevel *
100
).toInt()
currentVolume.value = volumePercent
},
colors = ButtonDefaults.buttonColors(backgroundColor = greenColor),
modifier = Modifier.padding(all = Dp(10F)),
enabled =
true
,
border = BorderStroke(width =
1
.dp, brush = SolidColor(Color.Blue)),
shape = MaterialTheme.shapes.medium,
) {
Text(text =
"-"
)
}
}
}
}