@@ -96,19 +96,15 @@ function initializeKaraoke() {
9696 button . addEventListener ( 'touchstart' , ( ) => addPressedEffect ( button ) , { passive : true } ) ;
9797 } ) ;
9898
99- // Add keyboard support for rewind and skip functionality
99+ // Add keyboard support for spacebar only
100100 document . addEventListener ( 'keydown' , ( event ) => {
101- if ( event . key === 'ArrowLeft' ) {
102- event . preventDefault ( ) ; // Prevent browser default behavior
103- rewindFiveSeconds ( ) ;
104- addPressedEffect ( document . getElementById ( 'restartBtn' ) ) ; // Visual feedback
105- console . log ( 'Left arrow key pressed - rewinding 5 seconds' ) ;
106- } else if ( event . key === 'ArrowRight' ) {
107- event . preventDefault ( ) ; // Prevent browser default behavior
108- skipForwardFiveSeconds ( ) ;
109- addPressedEffect ( document . getElementById ( 'playBtn' ) ) ; // Visual feedback on play button
110- console . log ( 'Right arrow key pressed - skipping forward 5 seconds' ) ;
111- } else if ( event . code === 'Space' ) {
101+ // Make sure lyricsEngine is initialized before handling keyboard events
102+ if ( ! lyricsEngine ) {
103+ console . warn ( 'Keyboard shortcut ignored - LyricsEngine not initialized' ) ;
104+ return ;
105+ }
106+
107+ if ( event . code === 'Space' ) {
112108 event . preventDefault ( ) ; // Prevent page scroll
113109 togglePlayPause ( ) ;
114110 console . log ( 'Spacebar pressed - toggling play/pause' ) ;
@@ -139,32 +135,47 @@ function stopLyrics() {
139135
140136// Rewind function for 5-second back functionality
141137function rewindFiveSeconds ( ) {
138+ if ( ! lyricsEngine ) {
139+ console . error ( 'LyricsEngine not initialized' ) ;
140+ return ;
141+ }
142+
142143 console . log ( 'Rewinding 5 seconds' ) ;
143144
144- // Pause both songs first
145- lyricsEngine . pause ( ) ;
145+ // Remember if we were playing before seeking
146+ const wasPlaying = ! lyricsEngine . isPaused ( ) ;
146147
147148 // Calculate new time (minimum 0 seconds)
148149 const newTime = Math . max ( 0 , lyricsEngine . getCurrentTime ( ) - 5 ) ;
149150
150151 // Set both audio tracks to new time
151152 lyricsEngine . setCurrentTime ( newTime ) ;
152153
153- // Brief pause before resuming (smoother transition)
154- setTimeout ( ( ) => {
155- // Resume playback
156- lyricsEngine . play ( ) ;
154+ // Only resume playback if we were already playing
155+ if ( wasPlaying ) {
156+ setTimeout ( ( ) => {
157+ lyricsEngine . play ( ) ;
158+ updatePlayButtonAppearance ( ) ;
159+ updateRestartButtonAppearance ( ) ;
160+ } , 100 ) ; // 100ms pause for smooth transition
161+ } else {
162+ // Just update button appearances for paused state
157163 updatePlayButtonAppearance ( ) ;
158164 updateRestartButtonAppearance ( ) ;
159- } , 100 ) ; // 100ms pause for smooth transition
165+ }
160166}
161167
162168// Skip forward function for 5-second ahead functionality
163169function skipForwardFiveSeconds ( ) {
170+ if ( ! lyricsEngine ) {
171+ console . error ( 'LyricsEngine not initialized' ) ;
172+ return ;
173+ }
174+
164175 console . log ( 'Skipping forward 5 seconds' ) ;
165176
166- // Pause both songs first
167- lyricsEngine . pause ( ) ;
177+ // Remember if we were playing before seeking
178+ const wasPlaying = ! lyricsEngine . isPaused ( ) ;
168179
169180 // Calculate new time (with upper bounds checking)
170181 const maxTime = lyricsEngine . getDuration ( ) || Infinity ;
@@ -173,13 +184,18 @@ function skipForwardFiveSeconds() {
173184 // Set both audio tracks to new time
174185 lyricsEngine . setCurrentTime ( newTime ) ;
175186
176- // Brief pause before resuming (smoother transition)
177- setTimeout ( ( ) => {
178- // Resume playback
179- lyricsEngine . play ( ) ;
187+ // Only resume playback if we were already playing
188+ if ( wasPlaying ) {
189+ setTimeout ( ( ) => {
190+ lyricsEngine . play ( ) ;
191+ updatePlayButtonAppearance ( ) ;
192+ updateRestartButtonAppearance ( ) ;
193+ } , 100 ) ; // 100ms pause for smooth transition
194+ } else {
195+ // Just update button appearances for paused state
180196 updatePlayButtonAppearance ( ) ;
181197 updateRestartButtonAppearance ( ) ;
182- } , 100 ) ; // 100ms pause for smooth transition
198+ }
183199}
184200
185201// Global functions for onclick handlers
0 commit comments