Skip to content

Commit

Permalink
fix/simplify: allow the tracklist to get large but loadLimit 3
Browse files Browse the repository at this point in the history
this avoids all the bugs that happen when you clear the track list or
remove songs while a song is still playing
  • Loading branch information
johnnyshankman committed Nov 16, 2024
1 parent 089cfe4 commit 37d07f6
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions src/renderer/store/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ const useMainStore = create<StoreState & StoreActions>((set) => ({
// Player state
player: new Gapless5({
useHTML5Audio: false,
loglevel: LogLevel.Debug,
crossfade: 100,
exclusive: true,
loadLimit: 3,
}),
paused: false,
currentSong: '',
Expand Down Expand Up @@ -263,10 +263,10 @@ const useMainStore = create<StoreState & StoreActions>((set) => ({
}

const tracks = state.player.getTracks();
if (tracks.length < 2) return {}; // Safety check
const index = state.player.getIndex();

// Get next song info before removing anything
const nextSongPath = tracks[1].replace(
const nextSongPath = tracks[index + 1].replace(
'my-magic-protocol://getMediaFile/',
'',
);
Expand All @@ -292,19 +292,14 @@ const useMainStore = create<StoreState & StoreActions>((set) => ({
state.player.pause();

// Remove current track and start playing next track
state.player.removeTrack(0);
state.player.next(0, 1, 0);

if (!state.paused) {
state.player.play();
const audioElement = document.querySelector('audio');
if (audioElement) {
audioElement.play();
}

// @note: sometimes the song doesn't play if we don't wait for the onload event
state.player.onload = () => {
state.player.play();
};
} else {
state.player.pause();
const audioElement = document.querySelector('audio');
Expand Down Expand Up @@ -376,11 +371,12 @@ const useMainStore = create<StoreState & StoreActions>((set) => ({
};
}

// this is already up by one
const tracks = state.player.getTracks();
if (tracks.length < 2) return {}; // Safety check
const index = state.player.getIndex();

// Get next song info before removing anything
const nextSongPath = tracks[1].replace(
const nextSongPath = tracks[index].replace(
'my-magic-protocol://getMediaFile/',
'',
);
Expand Down Expand Up @@ -437,19 +433,6 @@ const useMainStore = create<StoreState & StoreActions>((set) => ({
nextSongPath,
);

// In exactly 1s remove all tracks before the current playing track
window.setTimeout(() => {
const currentTrack = state.player.currentSource();
// find the index in the tracks array of the current track
const currentTrackIndex = state.player
.getTracks()
.findIndex((track) => track === currentTrack.audioPath);
// remove all tracks before the current track's index
for (let i = 0; i < currentTrackIndex; i += 1) {
state.player.removeTrack(i);
}
}, 1000);

return {
currentSong: nextSongPath,
currentSongMetadata: nextSongMetadata,
Expand Down

0 comments on commit 37d07f6

Please sign in to comment.