This commit is contained in:
2026-09-12 15:55:12 +08:00
parent 8414f36cca
commit e2cc793bdb
4 changed files with 398 additions and 124 deletions

View File

@@ -21,6 +21,7 @@ import cn.learningpad.oraltrainer.sdk.SentenceBoundaryApiCallback
import cn.learningpad.oraltrainer.sdk.SentenceBoundaryApiResult
import cn.learningpad.oraltrainer.sdk.TrainingMediaItem
import cn.learningpad.oraltrainer.sdk.TrainingVideoSummary
import kotlin.math.roundToInt
class MainActivity : Activity() {
private lateinit var sdk: OralTrainerSdk
@@ -362,12 +363,43 @@ class MainActivity : Activity() {
if (!progressBarDragging) {
bindings.timeText.text =
"${PlaybackFormat.time(snapshot.positionMs)} / ${PlaybackFormat.time(snapshot.durationMs)}"
bindings.progressBar.progress = PlaybackFormat.progress(snapshot)
bindings.progressBar.progress = if (controller.config.continuousPlayback) {
PlaybackFormat.progress(snapshot)
} else {
sentenceProgress(snapshot)
}
}
bindings.speedText.text = PlaybackFormat.speed(snapshot.playbackSpeed)
bindings.statusText.text = PlaybackFormat.status(snapshot)
}
private fun sentenceCount(): Int {
return controller.currentTrainingItem()?.sentences?.size?.takeIf { it > 0 }
?: screenState.currentSentenceCount
}
private fun sentenceProgress(snapshot: PlaybackSnapshot): Int {
val count = sentenceCount()
val index = snapshot.sentenceIndex ?: return 0
if (count <= 1) return 0
return (index * PROGRESS_MAX.toFloat() / (count - 1)).roundToInt()
.coerceIn(0, PROGRESS_MAX)
}
private fun sentenceForProgress(progress: Int): Triple<Int, SentenceBoundary?, Int> {
val sentences = controller.currentTrainingItem()?.sentences.orEmpty()
if (sentences.isEmpty()) {
return Triple(0, null, 0)
}
val index = if (sentences.size == 1) {
0
} else {
(progress.toFloat() / PROGRESS_MAX * (sentences.size - 1)).roundToInt()
.coerceIn(0, sentences.lastIndex)
}
return Triple(index, sentences.getOrNull(index), sentences.size)
}
private fun applySentence(sentence: SentenceBoundary?) {
if (!bindings.hasPlayerChrome) return
if (sentence == null) {
@@ -399,9 +431,21 @@ class MainActivity : Activity() {
override fun onSeekProgressPreview(progress: Int) {
if (!bindings.hasPlayerChrome) return
val duration = controller.snapshot().durationMs
if (controller.config.continuousPlayback) {
val duration = controller.snapshot().durationMs
bindings.timeText.text =
"${PlaybackFormat.time(PlaybackFormat.seekPosition(progress, duration))} / " +
PlaybackFormat.time(duration)
return
}
val (index, sentence, count) = sentenceForProgress(progress)
bindings.timeText.text =
"${PlaybackFormat.time(PlaybackFormat.seekPosition(progress, duration))} / ${PlaybackFormat.time(duration)}"
if (sentence == null) {
"暂无句子边界"
} else {
"第 ${index + 1}/$count 句 " +
"${PlaybackFormat.time(sentence.startMs)}-${PlaybackFormat.time(sentence.endMs)}"
}
}
override fun onSeekStart() {
@@ -410,9 +454,13 @@ class MainActivity : Activity() {
override fun onSeekStop(progress: Int) {
progressBarDragging = false
controller.seekTo(
PlaybackFormat.seekPosition(progress, controller.snapshot().durationMs),
)
if (controller.config.continuousPlayback) {
val duration = controller.snapshot().durationMs
controller.seekTo(PlaybackFormat.seekPosition(progress, duration))
return
}
val (_, sentence, _) = sentenceForProgress(progress)
sentence?.let { controller.seekTo(it.startMs) }
}
override fun onContinuousPlaybackChanged(enabled: Boolean) {