Files
ThreadNet-Web/src/components/views/audio_messages/PlaybackClock.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

75 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-04-27 20:27:36 -06:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
Copyright 2021-2023 The Matrix.org Foundation C.I.C.
2021-04-27 20:27:36 -06:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2021-04-27 20:27:36 -06:00
*/
import React from "react";
2021-10-22 17:23:32 -05:00
2021-04-27 20:27:36 -06:00
import Clock from "./Clock";
2025-02-05 13:25:06 +00:00
import { type Playback, PlaybackState } from "../../../audio/Playback";
import { UPDATE_EVENT } from "../../../stores/AsyncStore";
2021-04-27 20:27:36 -06:00
interface IProps {
playback: Playback;
2021-06-23 19:34:34 -06:00
// The default number of seconds to show when the playback has completed or
// has not started. Not used during playback, even when paused. Defaults to
// clip duration length.
defaultDisplaySeconds?: number;
2021-04-27 20:27:36 -06:00
}
interface IState {
seconds: number;
durationSeconds: number;
playbackPhase: PlaybackState;
}
/**
* A clock for a playback of a recording.
*/
export default class PlaybackClock extends React.PureComponent<IProps, IState> {
2023-02-13 11:39:16 +00:00
public constructor(props: IProps) {
2021-04-27 20:27:36 -06:00
super(props);
this.state = {
seconds: this.props.playback.clockInfo.timeSeconds,
// we track the duration on state because we won't really know what the clip duration
// is until the first time update, and as a PureComponent we are trying to dedupe state
// updates as much as possible. This is just the easiest way to avoid a forceUpdate() or
// member property to track "did we get a duration".
durationSeconds: this.props.playback.clockInfo.durationSeconds,
playbackPhase: PlaybackState.Stopped, // assume not started, so full clock
};
}
public componentDidMount(): void {
2021-04-27 20:27:36 -06:00
this.props.playback.on(UPDATE_EVENT, this.onPlaybackUpdate);
this.props.playback.clockInfo.liveData.onUpdate(this.onTimeUpdate);
}
private onPlaybackUpdate = (ev: PlaybackState): void => {
2021-04-27 20:27:36 -06:00
// Convert Decoding -> Stopped because we don't care about the distinction here
if (ev === PlaybackState.Decoding) ev = PlaybackState.Stopped;
2021-06-29 13:11:58 +01:00
this.setState({ playbackPhase: ev });
2021-04-27 20:27:36 -06:00
};
private onTimeUpdate = (time: number[]): void => {
2021-06-29 13:11:58 +01:00
this.setState({ seconds: time[0], durationSeconds: time[1] });
2021-04-27 20:27:36 -06:00
};
public render(): React.ReactNode {
2021-04-27 20:27:36 -06:00
let seconds = this.state.seconds;
if (this.state.playbackPhase === PlaybackState.Stopped) {
2021-06-23 19:34:34 -06:00
if (Number.isFinite(this.props.defaultDisplaySeconds)) {
2023-03-21 10:08:44 +01:00
seconds = this.props.defaultDisplaySeconds ?? this.props.playback.durationSeconds;
2021-06-23 19:34:34 -06:00
} else {
seconds = this.state.durationSeconds;
}
2021-04-27 20:27:36 -06:00
}
return <Clock seconds={seconds} role="timer" />;
2021-04-27 20:27:36 -06:00
}
}