Skip to content

Commit 28f9ba9

Browse files
committed
feat: autoplay and add current song to history when playback starts from the beginning.
1 parent 6ca204e commit 28f9ba9

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

client/src/stores/playerStore.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const usePlayerStore = create(
5353
audioElement.currentTime = 0;
5454
audioElement.src = '';
5555
}
56-
set({ currentSong: null, isPlaying: false });
56+
set({ currentSong: null, isPlaying: false, currentTime: 0 });
5757
document.title = 'SyncVibe';
5858
},
5959

@@ -216,7 +216,10 @@ export const usePlayerStore = create(
216216

217217
navigator.mediaSession.metadata = new MediaMetadata({
218218
title: song.name,
219-
artist: song?.artist_map?.artists?.slice(0, 3)?.map((a) => a.name).join(', '),
219+
artist: song?.artist_map?.artists
220+
?.slice(0, 3)
221+
?.map((a) => a.name)
222+
.join(', '),
220223
album: song?.album,
221224
artwork: song.image?.[2]?.link
222225
? [{ src: song.image[2].link, sizes: '500x500', type: 'image/jpeg' }]
@@ -237,19 +240,24 @@ export const usePlayerStore = create(
237240
},
238241

239242
loadAndPlayCurrentSong: async (prevSongId) => {
240-
const { currentSong, currentTime, isLoading, updateMediaSession, preloadNextTrack, _hasRestoredTime } = get();
243+
const { currentSong, currentTime, updateMediaSession, preloadNextTrack, _hasRestoredTime } =
244+
get();
241245
if (!audioElement || !currentSong || currentSong.id === prevSongId) {
242246
return prevSongId;
243247
}
244248
try {
245249
audioElement.src = getAudioUrl(currentSong);
246250
await audioElement.load();
247-
251+
248252
if (!_hasRestoredTime && currentTime > 0) {
249253
audioElement.currentTime = currentTime;
250254
set({ _hasRestoredTime: true });
255+
} else {
256+
await audioElement.play();
257+
set({ isPlaying: true, _hasRestoredTime: true });
258+
addToHistory(currentSong, 0, 'autoplay');
251259
}
252-
260+
253261
updateMediaSession(currentSong);
254262
preloadNextTrack();
255263
return currentSong.id;

0 commit comments

Comments
 (0)