diff --git a/app/src/main/java/com/keylightpiano/pianotimeline/ui/RecordingListViewModel.kt b/app/src/main/java/com/keylightpiano/pianotimeline/ui/RecordingListViewModel.kt index aa539e9..70bdffe 100644 --- a/app/src/main/java/com/keylightpiano/pianotimeline/ui/RecordingListViewModel.kt +++ b/app/src/main/java/com/keylightpiano/pianotimeline/ui/RecordingListViewModel.kt @@ -272,11 +272,15 @@ class RecordingListViewModel(app: Application) : AndroidViewModel(app) { } // Throttled persistence of the current track + offset: at most once per - // second, and only while a track is loaded. Saving live (every 200ms - // position tick) would hammer DataStore; 1s is plenty for resume. + // second, and only while a track is actively PLAYING. Saving while paused + // is both unnecessary (a paused position doesn't change) and harmful: on + // restore, preparePaused seeks asynchronously, so ExoPlayer briefly reports + // position 0 before the seek lands — saving during that window would clobber + // the good offset with 0. Gating on isPlaying closes that window. viewModelScope.launch { var lastSaveAt = 0L playbackState.collect { s -> + if (!s.isPlaying) return@collect val id = s.nowPlayingId ?: return@collect val now = System.currentTimeMillis() if (now - lastSaveAt >= 1000L) {