Fix: Specials-Rätsel spielen jetzt korrekt vom markierten Ausschnitt

- AudioPlayer setzt currentTime jetzt korrekt auf startTime beim Start
- Behebt Bug, bei dem Specials-Rätsel immer vom Anfang des Titels starteten
- Berücksichtigt startTime in togglePlay(), play() und autoPlay
This commit is contained in:
Hördle Bot
2026-01-24 12:29:03 +01:00
parent afbdb74516
commit cebdf7a5a2

View File

@@ -72,7 +72,10 @@ const AudioPlayer = forwardRef<AudioPlayerRef, AudioPlayerProps>(({ src, unlocke
if (autoPlay) { if (autoPlay) {
// Delay play slightly to ensure currentTime sticks // Delay play slightly to ensure currentTime sticks
setTimeout(() => { setTimeout(() => {
const playPromise = audioRef.current?.play(); if (audioRef.current) {
// Ensure currentTime is set before playing
audioRef.current.currentTime = startTime;
const playPromise = audioRef.current.play();
if (playPromise !== undefined) { if (playPromise !== undefined) {
playPromise playPromise
.then(() => { .then(() => {
@@ -86,8 +89,13 @@ const AudioPlayer = forwardRef<AudioPlayerRef, AudioPlayerProps>(({ src, unlocke
setIsPlaying(false); setIsPlaying(false);
}); });
} }
}
}, 150); }, 150);
} }
} else if (startTime !== undefined && audioRef.current.currentTime < startTime) {
// If startTime is set and currentTime is before it, reset to startTime
// This handles the case where the audio element was reset or reloaded
audioRef.current.currentTime = startTime;
} }
} }
}, [src, unlockedSeconds, startTime, autoPlay, processedSrc, processedUnlockedSeconds]); }, [src, unlockedSeconds, startTime, autoPlay, processedSrc, processedUnlockedSeconds]);
@@ -97,6 +105,16 @@ const AudioPlayer = forwardRef<AudioPlayerRef, AudioPlayerProps>(({ src, unlocke
play: () => { play: () => {
if (!audioRef.current) return; if (!audioRef.current) return;
// Check if we need to reset to startTime
const current = audioRef.current.currentTime;
const elapsed = current - startTime;
// Reset if: never played before, current position is before startTime, or we've exceeded the unlocked segment
if (!hasPlayedOnce || current < startTime || elapsed >= unlockedSeconds) {
// Reset to start of segment
audioRef.current.currentTime = startTime;
}
const playPromise = audioRef.current.play(); const playPromise = audioRef.current.play();
if (playPromise !== undefined) { if (playPromise !== undefined) {
playPromise playPromise
@@ -121,8 +139,20 @@ const AudioPlayer = forwardRef<AudioPlayerRef, AudioPlayerProps>(({ src, unlocke
if (isPlaying) { if (isPlaying) {
audioRef.current.pause(); audioRef.current.pause();
setIsPlaying(false);
} else { } else {
// Check if we need to reset to startTime
const current = audioRef.current.currentTime;
const elapsed = current - startTime;
// Reset if: never played before, current position is before startTime, or we've exceeded the unlocked segment
if (!hasPlayedOnce || current < startTime || elapsed >= unlockedSeconds) {
// Reset to start of segment
audioRef.current.currentTime = startTime;
}
audioRef.current.play(); audioRef.current.play();
setIsPlaying(true);
onPlay?.(); onPlay?.();
if (hasPlayedOnce) { if (hasPlayedOnce) {
@@ -132,7 +162,6 @@ const AudioPlayer = forwardRef<AudioPlayerRef, AudioPlayerProps>(({ src, unlocke
onHasPlayedChange?.(true); // Notify parent onHasPlayedChange?.(true); // Notify parent
} }
} }
setIsPlaying(!isPlaying);
}; };
const handleTimeUpdate = () => { const handleTimeUpdate = () => {