fixed a bug

This commit is contained in:
2026-09-12 20:27:42 +08:00
parent 93232a64fa
commit 96d0ff5318
2 changed files with 282 additions and 136 deletions

View File

@@ -11,7 +11,6 @@ import androidx.media3.common.MediaItem
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.PlayerMessage
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
import java.util.concurrent.CopyOnWriteArraySet
import kotlin.math.max
@@ -31,8 +30,10 @@ class OralTrainerController internal constructor(
private var lastSentenceIndex: Int? = null
private var stopAtSentenceEnd = false
private var stopAtSentenceEndIndex: Int? = null
private var boundaryMessage: PlayerMessage? = null
private var boundaryGuardToken = 0
private var baseItemIndex = 0
private var clippedSentence: SentenceBoundary? = null
private var updatingPlaybackWindow = false
private var unclippedDurationMs = -1L
var config: PlayerConfig = initialConfig
private set
@@ -47,10 +48,9 @@ class OralTrainerController internal constructor(
if (released) {
return
}
enforceSentenceBoundary()
notifySentenceIfChanged()
notifySnapshot()
mainHandler.postDelayed(this, 250L)
mainHandler.postDelayed(this, IDLE_POLL_MS)
}
}
@@ -66,24 +66,33 @@ class OralTrainerController internal constructor(
exoPlayer.playWhenReady = initialConfig.autoPlay
exoPlayer.addListener(object : Player.Listener {
override fun onPlaybackStateChanged(playbackState: Int) {
notifySnapshot()
}
override fun onIsPlayingChanged(isPlaying: Boolean) {
if (isPlaying) {
refreshSentenceBoundaryGuard()
} else {
cancelSentenceBoundaryGuard()
if (playbackState == Player.STATE_ENDED) {
onClippedSentenceEnded()
}
if (playbackState == Player.STATE_READY && unclippedDurationMs <= 0L) {
val duration = player.duration
if (duration != C.TIME_UNSET && clippedSentence == null) {
unclippedDurationMs = duration
}
}
notifySnapshot()
}
override fun onIsPlayingChanged(isPlaying: Boolean) {
notifySnapshot()
}
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
lastSentenceIndex = null
cancelSentenceBoundaryGuard()
if (updatingPlaybackWindow) {
notifySnapshot()
return
}
if (clippedSentence == null) {
baseItemIndex = player.currentMediaItemIndex
lastSentenceIndex = null
}
listeners.forEach { it.onMediaChanged(currentTrainingItem()) }
notifySentenceIfChanged(force = true)
refreshSentenceBoundaryGuard()
notifySnapshot()
}
@@ -93,7 +102,6 @@ class OralTrainerController internal constructor(
reason: Int,
) {
notifySentenceIfChanged(force = true)
refreshSentenceBoundaryGuard()
notifySnapshot()
}
@@ -140,9 +148,16 @@ class OralTrainerController internal constructor(
lastSentenceIndex = null
stopAtSentenceEnd = false
stopAtSentenceEndIndex = null
player.setMediaItems(items.map { it.toMedia3Item() }, safeIndex, max(0L, startPositionMs))
baseItemIndex = safeIndex
clippedSentence = null
unclippedDurationMs = -1L
val startMs = max(0L, startPositionMs)
player.setMediaItems(items.map { it.toMedia3Item() }, safeIndex, startMs)
player.prepare()
player.playWhenReady = config.autoPlay
applyLoopMode()
lockSentenceAt(startMs)
syncPlaybackWindow(startMs)
listeners.forEach { it.onMediaChanged(currentTrainingItem()) }
notifySentenceIfChanged(force = true)
notifySnapshot()
@@ -166,25 +181,26 @@ class OralTrainerController internal constructor(
return true
}
mediaItems[index] = updatedItem
if (index == player.currentMediaItemIndex) {
if (index == baseItemIndex) {
listeners.forEach { it.onMediaChanged(updatedItem) }
lockSentenceAt(playbackPositionMs())
syncPlaybackWindow()
notifySentenceIfChanged(force = true)
refreshSentenceBoundaryGuard()
notifySnapshot()
}
return true
}
fun play() {
syncPlaybackWindow()
player.play()
refreshSentenceBoundaryGuard()
}
fun pause() {
stopAtSentenceEnd = false
stopAtSentenceEndIndex = null
cancelSentenceBoundaryGuard()
player.pause()
applySentenceRepeatMode()
}
fun togglePlayPause() {
@@ -198,19 +214,22 @@ class OralTrainerController internal constructor(
fun stop() {
stopAtSentenceEnd = false
stopAtSentenceEndIndex = null
cancelSentenceBoundaryGuard()
player.stop()
}
fun seekTo(positionMs: Long) {
stopAtSentenceEnd = false
stopAtSentenceEndIndex = null
player.seekTo(max(0L, positionMs))
refreshSentenceBoundaryGuard()
val safePositionMs = max(0L, positionMs)
lockSentenceAt(safePositionMs)
syncPlaybackWindow(safePositionMs)
seekInCurrentWindow(safePositionMs)
notifySentenceIfChanged(force = true)
notifySnapshot()
}
fun seekRelative(deltaMs: Long) {
seekTo(player.currentPosition + deltaMs)
seekTo(playbackPositionMs() + deltaMs)
}
fun rewind(stepMs: Long = config.defaultSeekStepMs) {
@@ -222,11 +241,11 @@ class OralTrainerController internal constructor(
}
fun previousItem() {
player.seekToPreviousMediaItem()
switchBaseItem(baseItemIndex - 1)
}
fun nextItem() {
player.seekToNextMediaItem()
switchBaseItem(baseItemIndex + 1)
}
fun setPlaybackSpeed(speed: Float) {
@@ -240,20 +259,21 @@ class OralTrainerController internal constructor(
fun setLoopMode(loopMode: LoopMode) {
this.loopMode = loopMode
player.repeatMode = when (loopMode) {
LoopMode.OFF -> Player.REPEAT_MODE_OFF
LoopMode.ONE -> Player.REPEAT_MODE_ONE
LoopMode.ALL -> Player.REPEAT_MODE_ALL
}
applySentenceRepeatMode()
}
fun setSentenceMode(enabled: Boolean) {
config = config.copy(sentenceMode = enabled)
syncPlaybackWindow()
}
fun setContinuousPlayback(enabled: Boolean) {
config = config.copy(continuousPlayback = enabled)
refreshSentenceBoundaryGuard()
if (!enabled) {
lockSentenceAt(playbackPositionMs())
}
syncPlaybackWindow()
notifySentenceIfChanged(force = true)
}
fun playCurrentSentenceAndStop() {
@@ -265,22 +285,37 @@ class OralTrainerController internal constructor(
fun playSentenceAndStop(startMs: Long, sentenceIndex: Int) {
stopAtSentenceEnd = true
stopAtSentenceEndIndex = sentenceIndex
player.seekTo(max(0L, startMs))
lastSentenceIndex = sentenceIndex
val item = currentTrainingItem()
val sentence = item?.sentences?.getOrNull(sentenceIndex)
if (item != null && sentence != null) {
installSentenceClip(item, sentence, 0L)
} else {
player.seekTo(max(0L, startMs))
}
player.play()
refreshSentenceBoundaryGuard()
notifySentenceIfChanged(force = true)
notifySnapshot()
}
fun currentTrainingItem(): TrainingMediaItem? {
val index = player.currentMediaItemIndex
return mediaItems.getOrNull(index)
return mediaItems.getOrNull(baseItemIndex)
}
fun currentSentence(): SentenceBoundary? {
val item = currentTrainingItem() ?: return null
val position = player.currentPosition
return item.sentences.lastOrNull { sentence ->
position >= sentence.startMs && position < sentence.endMs
if (item.sentences.isEmpty()) {
return null
}
clippedSentence?.let { window ->
return item.sentences.getOrNull(window.index)
}
if (shouldBoundCurrentSentence()) {
lastSentenceIndex?.let { index ->
item.sentences.getOrNull(index)?.let { return it }
}
}
return sentenceAt(item, playbackPositionMs())
}
fun seekToPreviousSentence(): Boolean {
@@ -288,11 +323,16 @@ class OralTrainerController internal constructor(
if (item.sentences.isEmpty()) {
return false
}
val position = player.currentPosition
val target = item.sentences
.asReversed()
.firstOrNull { it.startMs < position - PREVIOUS_SENTENCE_TOLERANCE_MS }
?: item.sentences.first()
val target = if (shouldBoundCurrentSentence()) {
val index = anchoredSentenceIndex(item) ?: 0
item.sentences.getOrNull((index - 1).coerceAtLeast(0)) ?: item.sentences.first()
} else {
val position = playbackPositionMs()
item.sentences
.asReversed()
.firstOrNull { it.startMs < position - PREVIOUS_SENTENCE_TOLERANCE_MS }
?: item.sentences.first()
}
seekTo(target.startMs)
return true
}
@@ -302,10 +342,15 @@ class OralTrainerController internal constructor(
if (item.sentences.isEmpty()) {
return false
}
val position = player.currentPosition
val target = item.sentences.firstOrNull {
it.startMs > position + NEXT_SENTENCE_TOLERANCE_MS
} ?: return false
val target = if (shouldBoundCurrentSentence()) {
val index = anchoredSentenceIndex(item) ?: return false
item.sentences.getOrNull(index + 1) ?: return false
} else {
val position = playbackPositionMs()
item.sentences.firstOrNull {
it.startMs > position + NEXT_SENTENCE_TOLERANCE_MS
} ?: return false
}
seekTo(target.startMs)
return true
}
@@ -362,12 +407,16 @@ class OralTrainerController internal constructor(
fun snapshot(): PlaybackSnapshot {
val item = currentTrainingItem()
val duration = player.duration.takeUnless { it == C.TIME_UNSET } ?: -1L
val duration = when {
unclippedDurationMs > 0L -> unclippedDurationMs
clippedSentence != null -> clippedSentence?.endMs ?: -1L
else -> player.duration.takeUnless { it == C.TIME_UNSET } ?: -1L
}
return PlaybackSnapshot(
mediaId = item?.id,
positionMs = max(0L, player.currentPosition),
positionMs = playbackPositionMs(),
durationMs = duration,
bufferedPositionMs = max(0L, player.bufferedPosition),
bufferedPositionMs = playbackBufferedPositionMs(),
isPlaying = player.isPlaying,
playbackState = player.playbackState.toOralTrainerState(),
playbackSpeed = player.playbackParameters.speed,
@@ -380,7 +429,6 @@ class OralTrainerController internal constructor(
return
}
released = true
cancelSentenceBoundaryGuard()
mainHandler.removeCallbacks(ticker)
listeners.clear()
player.release()
@@ -400,127 +448,224 @@ class OralTrainerController internal constructor(
if (!force && index == lastSentenceIndex) {
return
}
val changed = index != lastSentenceIndex
lastSentenceIndex = index
listeners.forEach { it.onSentenceChanged(sentence) }
if (changed && !config.continuousPlayback && !stopAtSentenceEnd) {
refreshSentenceBoundaryGuard()
}
}
private fun enforceSentenceBoundary() {
if (!config.sentenceMode || !player.isPlaying) {
private fun syncPlaybackWindow(positionMs: Long = playbackPositionMs()) {
if (released || updatingPlaybackWindow) {
return
}
val item = currentTrainingItem() ?: return
if (item.sentences.isEmpty()) {
val item = currentTrainingItem()
val target = sentenceForWindow(item)
if (target == null || item == null) {
restoreUnclipped(positionMs)
return
}
val position = player.currentPosition
if (stopAtSentenceEnd) {
val index = stopAtSentenceEndIndex ?: return
val sentence = item.sentences.getOrNull(index) ?: return
if (position >= sentence.endMs) {
applySentenceEndAction(sentence)
}
val current = clippedSentence
if (current != null &&
current.index == target.index &&
current.startMs == target.startMs &&
current.endMs == target.endMs
) {
applySentenceRepeatMode()
return
}
if (config.continuousPlayback) {
return
}
val anchorIndex = lastSentenceIndex ?: currentSentence()?.index ?: return
val anchor = item.sentences.getOrNull(anchorIndex) ?: return
if (position >= anchor.endMs) {
applySentenceEndAction(anchor)
}
val relativeMs = (positionMs - target.startMs).coerceAtLeast(0L)
installSentenceClip(item, target, relativeMs)
}
private fun refreshSentenceBoundaryGuard() {
cancelSentenceBoundaryGuard()
if (released || !config.sentenceMode) {
return
private fun sentenceForWindow(item: TrainingMediaItem?): SentenceBoundary? {
if (item == null || !config.sentenceMode || item.sentences.isEmpty()) {
return null
}
if (!player.isPlaying && !player.playWhenReady) {
return
}
val item = currentTrainingItem() ?: return
if (item.sentences.isEmpty()) {
return
}
val sentence = when {
stopAtSentenceEnd -> item.sentences.getOrNull(stopAtSentenceEndIndex ?: return)
return when {
stopAtSentenceEnd -> item.sentences.getOrNull(stopAtSentenceEndIndex ?: return null)
config.continuousPlayback -> null
else -> {
val index = lastSentenceIndex ?: currentSentence()?.index
item.sentences.getOrNull(index ?: return)
val index = lastSentenceIndex ?: sentenceAt(item, playbackPositionMs())?.index
item.sentences.getOrNull(index ?: return null)
}
} ?: return
}
}
val mediaItemIndex = player.currentMediaItemIndex
if (player.currentPosition >= sentence.endMs) {
applySentenceEndAction(sentence)
private fun installSentenceClip(
item: TrainingMediaItem,
sentence: SentenceBoundary,
relativePositionMs: Long,
) {
if (released) {
return
}
val token = ++boundaryGuardToken
boundaryMessage = player.createMessage { _, _ ->
if (token != boundaryGuardToken || released) {
return@createMessage
}
val active = currentTrainingItem() ?: return@createMessage
val latest = when {
stopAtSentenceEnd -> active.sentences.getOrNull(stopAtSentenceEndIndex ?: return@createMessage)
else -> active.sentences.getOrNull(sentence.index)
} ?: return@createMessage
if (player.currentMediaItemIndex != mediaItemIndex) {
return@createMessage
}
applySentenceEndAction(latest)
}
.setLooper(mainHandler.looper)
.setPosition(mediaItemIndex, sentence.endMs)
.setDeleteAfterDelivery(true)
.send()
rememberUnclippedDuration()
val lengthMs = (sentence.endMs - sentence.startMs).coerceAtLeast(0L)
val startPositionMs = relativePositionMs.coerceIn(0L, lengthMs)
val playWhenReady = player.playWhenReady
updatingPlaybackWindow = true
clippedSentence = sentence
lastSentenceIndex = sentence.index
player.setMediaItem(item.toMedia3Item(sentence.startMs, sentence.endMs), startPositionMs)
player.prepare()
player.playWhenReady = playWhenReady
applySentenceRepeatMode()
updatingPlaybackWindow = false
}
private fun cancelSentenceBoundaryGuard() {
boundaryGuardToken++
boundaryMessage?.let { message ->
runCatching { message.cancel() }
private fun restoreUnclipped(positionMs: Long) {
if (released) {
return
}
boundaryMessage = null
if (clippedSentence == null && player.mediaItemCount == mediaItems.size) {
applyLoopMode()
return
}
val playWhenReady = player.playWhenReady
updatingPlaybackWindow = true
clippedSentence = null
player.setMediaItems(
mediaItems.map { it.toMedia3Item() },
baseItemIndex.coerceIn(0, mediaItems.lastIndex),
max(0L, positionMs),
)
player.prepare()
player.playWhenReady = playWhenReady
applyLoopMode()
updatingPlaybackWindow = false
}
private fun applySentenceEndAction(sentence: SentenceBoundary) {
private fun seekInCurrentWindow(positionMs: Long) {
val sentence = clippedSentence
if (sentence == null) {
player.seekTo(max(0L, positionMs))
return
}
val lengthMs = (sentence.endMs - sentence.startMs).coerceAtLeast(0L)
player.seekTo((positionMs - sentence.startMs).coerceIn(0L, lengthMs))
}
private fun onClippedSentenceEnded() {
val sentence = clippedSentence ?: return
player.seekTo(0L)
if (player.playbackState == Player.STATE_ENDED) {
player.prepare()
}
if (stopAtSentenceEnd && stopAtSentenceEndIndex == sentence.index) {
stopAtSentenceEnd = false
stopAtSentenceEndIndex = null
cancelSentenceBoundaryGuard()
player.pause()
player.seekTo(sentence.startMs)
lastSentenceIndex = sentence.index
notifySentenceIfChanged(force = true)
notifySnapshot()
return
applySentenceRepeatMode()
} else if (!config.continuousPlayback) {
player.play()
}
if (config.continuousPlayback) {
return
}
// Loop the current sentence without leaking into the next one.
player.seekTo(sentence.startMs)
lastSentenceIndex = sentence.index
notifySentenceIfChanged(force = true)
refreshSentenceBoundaryGuard()
notifySnapshot()
}
private fun TrainingMediaItem.toMedia3Item(): MediaItem {
private fun switchBaseItem(targetIndex: Int) {
if (targetIndex !in mediaItems.indices) {
return
}
baseItemIndex = targetIndex
lastSentenceIndex = null
stopAtSentenceEnd = false
stopAtSentenceEndIndex = null
clippedSentence = null
unclippedDurationMs = -1L
val item = mediaItems[targetIndex]
val firstSentence = item.sentences.firstOrNull()
if (shouldBoundCurrentSentence() && firstSentence != null) {
lastSentenceIndex = firstSentence.index
installSentenceClip(item, firstSentence, 0L)
} else {
player.setMediaItem(item.toMedia3Item(), 0L)
player.prepare()
applyLoopMode()
}
listeners.forEach { it.onMediaChanged(item) }
notifySentenceIfChanged(force = true)
notifySnapshot()
}
private fun playbackPositionMs(): Long {
val sentence = clippedSentence ?: return max(0L, player.currentPosition)
val lengthMs = (sentence.endMs - sentence.startMs).coerceAtLeast(0L)
return sentence.startMs + player.currentPosition.coerceIn(0L, lengthMs)
}
private fun playbackBufferedPositionMs(): Long {
val sentence = clippedSentence ?: return max(0L, player.bufferedPosition)
val lengthMs = (sentence.endMs - sentence.startMs).coerceAtLeast(0L)
return sentence.startMs + player.bufferedPosition.coerceIn(0L, lengthMs)
}
private fun shouldBoundCurrentSentence(): Boolean {
return config.sentenceMode && (!config.continuousPlayback || stopAtSentenceEnd)
}
private fun lockSentenceAt(positionMs: Long) {
if (!shouldBoundCurrentSentence()) {
return
}
val item = currentTrainingItem() ?: return
lastSentenceIndex = sentenceAt(item, positionMs)?.index
}
private fun anchoredSentenceIndex(item: TrainingMediaItem): Int? {
return lastSentenceIndex?.takeIf { it in item.sentences.indices }
?: clippedSentence?.index
?: sentenceAt(item, playbackPositionMs())?.index
}
private fun sentenceAt(item: TrainingMediaItem, positionMs: Long): SentenceBoundary? {
return item.sentences.lastOrNull { positionMs >= it.startMs && positionMs < it.endMs }
?: item.sentences.lastOrNull { it.startMs <= positionMs }
}
private fun rememberUnclippedDuration() {
if (unclippedDurationMs > 0L || clippedSentence != null) {
return
}
val duration = player.duration
if (duration != C.TIME_UNSET) {
unclippedDurationMs = duration
}
}
private fun applyLoopMode() {
player.repeatMode = when (loopMode) {
LoopMode.OFF -> Player.REPEAT_MODE_OFF
LoopMode.ONE -> Player.REPEAT_MODE_ONE
LoopMode.ALL -> Player.REPEAT_MODE_ALL
}
}
private fun applySentenceRepeatMode() {
if (clippedSentence == null) {
applyLoopMode()
return
}
player.repeatMode = Player.REPEAT_MODE_OFF
}
private fun TrainingMediaItem.toMedia3Item(
clippingStartPositionMs: Long = 0L,
clippingEndPositionMs: Long? = null,
): MediaItem {
val builder = MediaItem.Builder()
.setMediaId(id)
.setUri(uri)
mimeType?.let(builder::setMimeType)
customCacheKey?.let(builder::setCustomCacheKey)
if (clippingEndPositionMs != null) {
builder.setClippingConfiguration(
MediaItem.ClippingConfiguration.Builder()
.setStartPositionMs(max(0L, clippingStartPositionMs))
.setEndPositionMs(max(0L, clippingEndPositionMs))
.setStartsAtKeyFrame(false)
.build()
)
}
subtitleUri?.let { uri ->
builder.setSubtitleConfigurations(
listOf(
@@ -552,5 +697,6 @@ class OralTrainerController internal constructor(
private companion object {
const val PREVIOUS_SENTENCE_TOLERANCE_MS = 250L
const val NEXT_SENTENCE_TOLERANCE_MS = 150L
const val IDLE_POLL_MS = 250L
}
}