@@ -4,10 +4,8 @@ import { useShallow } from 'zustand/react/shallow';
44import { ensureHttpsForDownloadUrls , addToHistory } from '@/Pages/Music/Common' ;
55import axios from 'axios' ;
66
7- /* singletons */
87let audioElement = null ;
98let nextAudioElement = null ;
10- let progressAutoSaveInterval = null ;
119
1210const getAudioUrl = ( song ) =>
1311 song ?. download_url ?. [ 4 ] ?. link || song ?. download_url ?. [ 3 ] ?. link || '' ;
@@ -47,24 +45,10 @@ export const usePlayerStore = create(
4745 set ( { savedTime : Math . floor ( currentTime ) } ) ;
4846 } ,
4947
50- startAutoSave : ( ) => {
51- if ( progressAutoSaveInterval ) return ;
52- progressAutoSaveInterval = setInterval ( ( ) => {
53- const { currentTime } = get ( ) ;
54- set ( { savedTime : Math . floor ( currentTime ) } ) ;
55- } , 20000 ) ;
56- } ,
57-
58- stopAutoSave : ( ) => {
59- if ( progressAutoSaveInterval ) {
60- clearInterval ( progressAutoSaveInterval ) ;
61- progressAutoSaveInterval = null ;
62- }
63- } ,
64-
6548 playSong : ( song ) => {
6649 if ( ! song ?. id ) return ;
6750 const { saveProgress, playlist } = get ( ) ;
51+
6852 saveProgress ( ) ;
6953 set ( { isLoading : true , _hasRestoredTime : false } ) ;
7054
@@ -76,9 +60,8 @@ export const usePlayerStore = create(
7660 } ,
7761
7862 stopSong : ( ) => {
79- const { stopAutoSave , saveProgress } = get ( ) ;
63+ const { saveProgress } = get ( ) ;
8064 saveProgress ( ) ;
81- stopAutoSave ( ) ;
8265
8366 if ( audioElement ) {
8467 audioElement . pause ( ) ;
@@ -97,16 +80,14 @@ export const usePlayerStore = create(
9780 } ,
9881
9982 handlePlayPause : ( ) => {
100- const { isPlaying, saveProgress, startAutoSave , stopAutoSave } = get ( ) ;
83+ const { isPlaying, saveProgress } = get ( ) ;
10184 if ( ! audioElement ) return ;
10285
10386 if ( isPlaying ) {
10487 audioElement . pause ( ) ;
10588 saveProgress ( ) ;
106- stopAutoSave ( ) ;
10789 } else {
10890 audioElement . play ( ) . catch ( console . error ) ;
109- startAutoSave ( ) ;
11091 }
11192
11293 set ( { isPlaying : ! isPlaying } ) ;
@@ -134,6 +115,7 @@ export const usePlayerStore = create(
134115 handleNextSong : ( ) => {
135116 const { currentSong, playlist, playSong, saveProgress } = get ( ) ;
136117 if ( ! currentSong || ! playlist . length ) return ;
118+
137119 saveProgress ( ) ;
138120 const idx = playlist . findIndex ( ( s ) => s . id === currentSong . id ) ;
139121 playSong ( playlist [ ( idx + 1 ) % playlist . length ] ) ;
@@ -142,6 +124,7 @@ export const usePlayerStore = create(
142124 handlePrevSong : ( ) => {
143125 const { currentSong, playlist, playSong, saveProgress } = get ( ) ;
144126 if ( ! currentSong || ! playlist . length ) return ;
127+
145128 saveProgress ( ) ;
146129 const idx = playlist . findIndex ( ( s ) => s . id === currentSong . id ) ;
147130 playSong ( playlist [ ( idx - 1 + playlist . length ) % playlist . length ] ) ;
@@ -150,6 +133,7 @@ export const usePlayerStore = create(
150133 preloadNextTrack : ( ) => {
151134 const { playlist, currentSong } = get ( ) ;
152135 if ( ! nextAudioElement || ! currentSong || ! playlist . length ) return ;
136+
153137 const idx = playlist . findIndex ( ( s ) => s . id === currentSong . id ) ;
154138 const next = playlist [ ( idx + 1 ) % playlist . length ] ;
155139 if ( next ) {
@@ -186,7 +170,6 @@ export const usePlayerStore = create(
186170 _hasRestoredTime,
187171 updateMediaSession,
188172 preloadNextTrack,
189- startAutoSave,
190173 } = get ( ) ;
191174
192175 if ( ! audioElement || ! currentSong || currentSong . id === prevSongId ) {
@@ -195,15 +178,13 @@ export const usePlayerStore = create(
195178
196179 try {
197180 audioElement . src = getAudioUrl ( currentSong ) ;
198- await audioElement . load ( ) ;
199181
200182 if ( ! _hasRestoredTime && savedTime > 0 ) {
201183 audioElement . currentTime = savedTime ;
202184 set ( { currentTime : savedTime , _hasRestoredTime : true } ) ;
203185 }
204186
205187 await audioElement . play ( ) ;
206- startAutoSave ( ) ;
207188
208189 set ( { isPlaying : true } ) ;
209190 addToHistory ( currentSong , audioElement . currentTime , 'autoplay' ) ;
@@ -260,7 +241,6 @@ export const usePlayerControls = () =>
260241 handleNextSong : s . handleNextSong ,
261242 handlePrevSong : s . handlePrevSong ,
262243 handleTimeSeek : s . handleTimeSeek ,
263- handleVolumeChange : s . handleVolumeChange ,
264244 addToQueue : s . addToQueue ,
265245 addToPlaylist : s . setPlaylist ,
266246 } ) )
@@ -302,4 +282,4 @@ export const useIsPlaying = () => usePlayerStore((s) => s.isPlaying);
302282export const usePlaylist = ( ) => usePlayerStore ( ( s ) => s . playlist ) ;
303283export const useVolume = ( ) => usePlayerStore ( ( s ) => s . volume ) ;
304284
305- export { getAudioUrl } ;
285+ export { getAudioUrl } ;
0 commit comments