new: allow user to seek a position in the record
This commit is contained in:
@@ -18,14 +18,17 @@ import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.LinearProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Slider
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
@@ -58,6 +61,7 @@ fun PlayerScreen(
|
||||
recordTitle = recordTitle,
|
||||
uiState = uiState,
|
||||
onMainPlayActionButtonClicked = viewModel::performMainPlayAction,
|
||||
onSeek = viewModel::seekTo,
|
||||
onBackClicked = onBackClicked,
|
||||
)
|
||||
}
|
||||
@@ -68,6 +72,7 @@ fun PlayerScreen(
|
||||
recordTitle: String,
|
||||
uiState: PlayerUiState,
|
||||
onMainPlayActionButtonClicked: () -> Unit,
|
||||
onSeek: (progress: Float) -> Unit,
|
||||
onBackClicked: () -> Unit,
|
||||
) {
|
||||
Scaffold(
|
||||
@@ -103,8 +108,18 @@ fun PlayerScreen(
|
||||
|
||||
Spacer(modifier = Modifier.weight(1f))
|
||||
|
||||
LinearProgressIndicator(
|
||||
progress = { uiState.progress },
|
||||
var seekProgress by remember { mutableStateOf<Float?>(null) }
|
||||
Slider(
|
||||
value = seekProgress ?: uiState.progress,
|
||||
onValueChange = { seekProgress = it },
|
||||
onValueChangeFinished = {
|
||||
seekProgress?.let { fraction ->
|
||||
if (uiState.durationMs > 0) {
|
||||
onSeek(fraction)
|
||||
}
|
||||
}
|
||||
seekProgress = null
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
|
||||
@@ -145,6 +160,7 @@ private fun PlayerScreenPlayingPreview() {
|
||||
recordTitle = "Demat",
|
||||
uiState = PlayerUiState(playbackState = PlaybackState.PLAYING, positionMs = 1234, durationMs = 5000),
|
||||
onMainPlayActionButtonClicked = {},
|
||||
onSeek = {},
|
||||
onBackClicked = {},
|
||||
)
|
||||
}
|
||||
@@ -158,6 +174,7 @@ private fun PlayerScreenPausedPreview() {
|
||||
recordTitle = "Demat",
|
||||
uiState = PlayerUiState(playbackState = PlaybackState.PAUSED, positionMs = 1234, durationMs = 5000),
|
||||
onMainPlayActionButtonClicked = {},
|
||||
onSeek = {},
|
||||
onBackClicked = {},
|
||||
)
|
||||
}
|
||||
@@ -171,6 +188,7 @@ private fun PlayerScreenEndedPreview() {
|
||||
recordTitle = "Demat",
|
||||
uiState = PlayerUiState(playbackState = PlaybackState.ENDED, positionMs = 1234, durationMs = 5000),
|
||||
onMainPlayActionButtonClicked = {},
|
||||
onSeek = {},
|
||||
onBackClicked = {},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -81,6 +81,11 @@ class PlayerViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
fun seekTo(progress: Float) {
|
||||
val position = (progress.toDouble() * uiState.value.durationMs).milliseconds
|
||||
audioPlayer.seekTo(position)
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
audioPlayer.stop()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user