Skip to content

Commit 02f3275

Browse files
committed
Fix bugs seeking while paused; and next track resetting progress
1 parent 04ae006 commit 02f3275

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

src/queueMachine.ts

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ export const createQueueMachine = setup({
145145
const nextActor = context.trackActors[nextIdx];
146146
if (nextActor) {
147147
console.log(`🚀 QUEUE: Activating and playing track ${nextIdx}`);
148-
// Directly activate and play the next track
148+
// Reset track position to start and then activate and play
149+
nextActor.send({ type: 'SEEK', time: 0 });
149150
nextActor.send({ type: 'ACTIVATE' });
150151
nextActor.send({ type: 'PLAY' });
151152
}
@@ -256,11 +257,18 @@ export const createQueueMachine = setup({
256257
currentTrackIdx: 0,
257258
}),
258259
setVolume: assign({
259-
volume: ({ event }) => {
260-
if (event.type === 'SET_VOLUME') {
261-
return Math.max(0, Math.min(1, event.volume));
262-
}
263-
return 1;
260+
volume: ({ context, event }) => {
261+
if (event.type !== 'SET_VOLUME') return context.volume;
262+
263+
const newVolume = Math.max(0, Math.min(1, event.volume));
264+
console.log(`🔊 QUEUE: Setting volume to ${(newVolume * 100).toFixed(0)}%`);
265+
266+
// Send volume update to all track actors
267+
context.trackActors.forEach((trackActor) => {
268+
trackActor.send({ type: 'SET_VOLUME', volume: newVolume });
269+
});
270+
271+
return newVolume;
264272
},
265273
}),
266274
spawnInitialTracks: assign({
@@ -459,36 +467,42 @@ export const createQueueMachine = setup({
459467
if (typeof window !== 'undefined' && 'mediaSession' in navigator) {
460468
const currentTrackMeta = context.trackMetadata?.[context.currentTrackIdx];
461469
const defaultTitle = `Track ${context.currentTrackIdx + 1}`;
462-
470+
463471
navigator.mediaSession.metadata = new MediaMetadata({
464472
title: currentTrackMeta?.title || defaultTitle,
465473
artist: currentTrackMeta?.artist || 'Unknown Artist',
466474
album: currentTrackMeta?.album || 'Gapless Playlist',
467-
artwork: currentTrackMeta?.artwork ? [
468-
{ src: currentTrackMeta.artwork, sizes: '96x96', type: 'image/png' },
469-
{ src: currentTrackMeta.artwork, sizes: '128x128', type: 'image/png' },
470-
{ src: currentTrackMeta.artwork, sizes: '192x192', type: 'image/png' },
471-
{ src: currentTrackMeta.artwork, sizes: '256x256', type: 'image/png' },
472-
{ src: currentTrackMeta.artwork, sizes: '384x384', type: 'image/png' },
473-
{ src: currentTrackMeta.artwork, sizes: '512x512', type: 'image/png' },
474-
] : []
475+
artwork: currentTrackMeta?.artwork
476+
? [
477+
{ src: currentTrackMeta.artwork, sizes: '96x96', type: 'image/png' },
478+
{ src: currentTrackMeta.artwork, sizes: '128x128', type: 'image/png' },
479+
{ src: currentTrackMeta.artwork, sizes: '192x192', type: 'image/png' },
480+
{ src: currentTrackMeta.artwork, sizes: '256x256', type: 'image/png' },
481+
{ src: currentTrackMeta.artwork, sizes: '384x384', type: 'image/png' },
482+
{ src: currentTrackMeta.artwork, sizes: '512x512', type: 'image/png' },
483+
]
484+
: [],
475485
});
476486

477487
console.log('📱 DEVICE: Updated MediaSession metadata', {
478488
title: currentTrackMeta?.title || defaultTitle,
479-
artist: currentTrackMeta?.artist || 'Unknown Artist'
489+
artist: currentTrackMeta?.artist || 'Unknown Artist',
480490
});
481491
}
482492
},
483493

484494
updateMediaSessionPlaybackState: ({ context }) => {
485495
if (typeof window !== 'undefined' && 'mediaSession' in navigator) {
486496
// Get current state from context or derive it
487-
const isPlaying = context.trackActors.some(actor => {
497+
const isPlaying = context.trackActors.some((actor) => {
488498
const snapshot = actor.getSnapshot();
489-
return snapshot.status === 'active' && snapshot.matches && snapshot.matches({ playback: 'playing' });
499+
return (
500+
snapshot.status === 'active' &&
501+
snapshot.matches &&
502+
snapshot.matches({ playback: 'playing' })
503+
);
490504
});
491-
505+
492506
navigator.mediaSession.playbackState = isPlaying ? 'playing' : 'paused';
493507
console.log('📱 DEVICE: Updated playback state to', navigator.mediaSession.playbackState);
494508
}
@@ -502,7 +516,12 @@ export const createQueueMachine = setup({
502516
}).createMachine({
503517
id: 'queue',
504518
initial: 'paused',
505-
entry: ['spawnInitialTracks', 'loadCurrentTrack', 'setupMediaSession', 'updateMediaSessionMetadata'],
519+
entry: [
520+
'spawnInitialTracks',
521+
'loadCurrentTrack',
522+
'setupMediaSession',
523+
'updateMediaSessionMetadata',
524+
],
506525
context: ({ input }) => ({
507526
tracks: input?.tracks || [],
508527
currentTrackIdx: 0,

src/trackMachine.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,12 @@ export const createTrackMachine = setup({
361361
createAndStartBufferSource(context, self, time);
362362
} else {
363363
console.log('🎯 TRACK: WebAudio seek - track was paused, position updated only');
364+
// Trigger progress update even when paused to update UI
365+
self.send({ type: 'PROGRESS' });
364366
}
365367
},
366368

367-
seekHTML5: ({ context, event }) => {
369+
seekHTML5: ({ context, event, self }) => {
368370
if (event.type !== 'SEEK') return;
369371
const time = Math.max(0, event.time);
370372
console.log(`🎯 TRACK: HTML5 seek to ${time.toFixed(2)}s`);
@@ -373,6 +375,8 @@ export const createTrackMachine = setup({
373375
context.audio.currentTime = time;
374376
context.currentTime = time;
375377
console.log(`🎯 TRACK: HTML5 seek - updated currentTime to ${time.toFixed(2)}s`);
378+
// Trigger progress update to update UI
379+
self.send({ type: 'PROGRESS' });
376380
} else {
377381
console.warn('🎯 TRACK: HTML5 seek failed - no audio element');
378382
}
@@ -604,6 +608,9 @@ export const createTrackMachine = setup({
604608
actions: 'seekHTML5',
605609
},
606610
],
611+
PROGRESS: {
612+
actions: ['updateProgress', 'checkPreloadNext'],
613+
},
607614
},
608615
},
609616
playing: {

0 commit comments

Comments
 (0)