Fix duration of voice message in timeline (#30973)

* fix: duration of voice message in timeline

* Revert "Fix clocks rendering at 00:00 when playback had not begun."

This reverts commit 68bcfbed3e37e0a8c9529e92a3e17a5bc70bf7ed.

* refactor: cleaner clock states check

* refactor: cleaner `onPlaybackStateChange` condition

* fix: `timeSeconds` is always a number

* refactor: allow playing and paused state to update clock state

* test: add test

* test: add moar test

* refactor: use `currentClockState`
This commit is contained in:
Florian Duros
2025-10-09 11:10:57 +02:00
committed by GitHub
parent 4db6ff578d
commit b45488fc84
3 changed files with 50 additions and 37 deletions

View File

@@ -74,7 +74,7 @@ export class PlaybackClock implements IDestroyable {
// remainder of the division operation, we're assuming that playback is
// incomplete or stopped, thus giving an accurate position within the active
// clip segment.
return (this.context.currentTime - this.clipStart) % this.clipDuration;
return (this.context.currentTime - this.clipStart) % this.clipDuration || 0;
}
public get liveData(): SimpleObservable<number[]> {

View File

@@ -6,7 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
Please see LICENSE files in the repository root for full details.
*/
import { type MatrixEvent, type Room, EventType } from "matrix-js-sdk/src/matrix";
import { EventType, type MatrixEvent, type Room } from "matrix-js-sdk/src/matrix";
import { logger } from "matrix-js-sdk/src/logger";
import { type Playback, PlaybackState } from "./Playback";
@@ -76,6 +76,12 @@ export class PlaybackQueue {
const val = localStorage.getItem(`mx_voice_message_clocks_${this.room.roomId}`);
if (!!val) {
this.clockStates = new Map<string, number>(JSON.parse(val));
// Clean out any null values (from older versions)
for (const [key, value] of this.clockStates.entries()) {
if (value == null) {
this.clockStates.delete(key);
}
}
}
}
@@ -90,14 +96,10 @@ export class PlaybackQueue {
// Remember where the user got to in playback
const wasLastPlaying = this.currentPlaybackId === mxEvent.getId();
const currentClockState = this.clockStates.get(mxEvent.getId()!);
if (newState === PlaybackState.Stopped && currentClockState !== undefined && !wasLastPlaying) {
if (currentClockState > 0) {
// skipTo will pause playback, which causes the clock to render the current
// playback seconds. If the clock state is 0, then we can just ignore
// skipping entirely.
// noinspection JSIgnoredPromiseFromCall
playback.skipTo(currentClockState);
}
// noinspection JSIgnoredPromiseFromCall
playback.skipTo(currentClockState);
} else if (newState === PlaybackState.Stopped) {
// Remove the now-useless clock for some space savings
this.clockStates.delete(mxEvent.getId()!);
@@ -207,10 +209,8 @@ export class PlaybackQueue {
}
private onPlaybackClock(playback: Playback, mxEvent: MatrixEvent, clocks: number[]): void {
if (playback.currentState === PlaybackState.Decoding) return; // ignore pre-ready values
if (playback.currentState !== PlaybackState.Playing && playback.currentState !== PlaybackState.Paused) return; // ignore pre-ready values
if (playback.currentState !== PlaybackState.Stopped) {
this.clockStates.set(mxEvent.getId()!, clocks[0]); // [0] is the current seek position
}
this.clockStates.set(mxEvent.getId()!, clocks[0]); // [0] is the current seek position
}
}