@@ -2,6 +2,7 @@ import { useRef, useEffect, useState, useCallback } from "react";
22import { usePlayerStore } from "../../stores/playerStore" ;
33import { toast } from "react-hot-toast" ;
44import { Play , Pause } from "lucide-react" ;
5+ import { useShallow } from "zustand/react/shallow" ;
56
67interface MediaPlayerProps {
78 hiddenMode ?: boolean ;
@@ -16,6 +17,7 @@ export const MediaPlayer = ({ hiddenMode = false }: MediaPlayerProps) => {
1617 const isDelayingRef = useRef ( false ) ;
1718 // Track pending play intent so we can start playback once the element is ready
1819 const pendingPlayRef = useRef ( false ) ;
20+ const lastReportedTimeRef = useRef ( 0 ) ;
1921
2022 const {
2123 currentFile,
@@ -32,7 +34,24 @@ export const MediaPlayer = ({ hiddenMode = false }: MediaPlayerProps) => {
3234 setCurrentTime,
3335 setDuration,
3436 setIsPlaying,
35- } = usePlayerStore ( ) ;
37+ } = usePlayerStore (
38+ useShallow ( ( state ) => ( {
39+ currentFile : state . currentFile ,
40+ isPlaying : state . isPlaying ,
41+ currentTime : state . currentTime ,
42+ volume : state . volume ,
43+ mediaVolume : state . mediaVolume ,
44+ muted : state . muted ,
45+ playbackRate : state . playbackRate ,
46+ loopStart : state . loopStart ,
47+ loopEnd : state . loopEnd ,
48+ isLooping : state . isLooping ,
49+ showWaveform : state . showWaveform ,
50+ setCurrentTime : state . setCurrentTime ,
51+ setDuration : state . setDuration ,
52+ setIsPlaying : state . setIsPlaying ,
53+ } ) )
54+ ) ;
3655
3756 // Keep local state in sync with global state
3857 useEffect ( ( ) => {
@@ -234,7 +253,10 @@ export const MediaPlayer = ({ hiddenMode = false }: MediaPlayerProps) => {
234253
235254 const handleTimeUpdate = ( ) => {
236255 const currentTimeValue = mediaElement . currentTime ;
237- setCurrentTime ( currentTimeValue ) ;
256+ if ( Math . abs ( currentTimeValue - lastReportedTimeRef . current ) >= 0.05 ) {
257+ lastReportedTimeRef . current = currentTimeValue ;
258+ setCurrentTime ( currentTimeValue ) ;
259+ }
238260
239261 // Handle A-B looping
240262 if ( isLooping && loopStart !== null && loopEnd !== null ) {
@@ -311,14 +333,10 @@ export const MediaPlayer = ({ hiddenMode = false }: MediaPlayerProps) => {
311333 }
312334 } ;
313335
314- // Use less frequent checking to rely more on native timeupdate
315- const checkInterval = setInterval ( handleTimeUpdate , 100 ) ;
316-
317336 // Also keep the timeupdate event for standard time tracking
318337 mediaElement . addEventListener ( "timeupdate" , handleTimeUpdate ) ;
319338
320339 return ( ) => {
321- clearInterval ( checkInterval ) ;
322340 mediaElement . removeEventListener ( "timeupdate" , handleTimeUpdate ) ;
323341 } ;
324342 } , [ currentFile , isLooping , loopStart , loopEnd , setCurrentTime ] ) ;
0 commit comments