feat: proper video playback including images and videos on the timeline

This commit is contained in:
Hyteq
2025-06-23 14:40:34 +03:00
parent 7b6ab8f152
commit 61c172c9cc
4 changed files with 188 additions and 47 deletions

View File

@ -6,24 +6,67 @@ interface PlaybackStore extends PlaybackState, PlaybackControls {
setCurrentTime: (time: number) => void;
}
let playbackTimer: NodeJS.Timeout | null = null;
const startTimer = (store: any) => {
if (playbackTimer) clearInterval(playbackTimer);
playbackTimer = setInterval(() => {
const state = store();
if (state.isPlaying && state.currentTime < state.duration) {
const newTime = state.currentTime + 0.1;
if (newTime >= state.duration) {
state.pause();
} else {
state.setCurrentTime(newTime);
// Notify video elements to sync
window.dispatchEvent(new CustomEvent('playback-update', { detail: { time: newTime } }));
}
}
}, 100);
};
const stopTimer = () => {
if (playbackTimer) {
clearInterval(playbackTimer);
playbackTimer = null;
}
};
export const usePlaybackStore = create<PlaybackStore>((set, get) => ({
isPlaying: false,
currentTime: 0,
duration: 0,
volume: 1,
play: () => set({ isPlaying: true }),
pause: () => set({ isPlaying: false }),
toggle: () => set((state) => ({ isPlaying: !state.isPlaying })),
play: () => {
set({ isPlaying: true });
startTimer(get);
},
pause: () => {
set({ isPlaying: false });
stopTimer();
},
toggle: () => {
const { isPlaying } = get();
if (isPlaying) {
get().pause();
} else {
get().play();
}
},
seek: (time: number) => {
const { duration } = get();
const clampedTime = Math.max(0, Math.min(duration, time));
set({ currentTime: clampedTime });
// Notify video element to seek
const event = new CustomEvent('playback-seek', { detail: { time: clampedTime } });
window.dispatchEvent(event);
// Notify video elements to seek
window.dispatchEvent(new CustomEvent('playback-seek', { detail: { time: clampedTime } }));
},
setVolume: (volume: number) => set({ volume: Math.max(0, Math.min(1, volume)) }),
setDuration: (duration: number) => set({ duration }),
setCurrentTime: (time: number) => set({ currentTime: time }),