Fix media-key next replaying same song in background - #1
Closed
Endiruslan wants to merge 1 commit into
Closed
Conversation
In nextPreviousTrack mode, Kaset overrides the WebView's navigator.mediaSession 'nexttrack'/'previoustrack' handlers so media keys advance the native queue (via REMOTE_NEXT). YouTube Music periodically re-registers its own handlers, and Kaset re-asserts its override through a requestAnimationFrame loop on the page. WebKit freezes requestAnimationFrame while the app is backgrounded, so the override was never re-applied. After some time backgrounded a media-key press fell through to YouTube's own handler, which jumped to YouTube's recommended video. Queue-drift recovery then saw a video not in the native queue and force-reloaded the current queue song from 0 — the same song appeared to restart. Drive the override re-assertion from a native run-loop Timer instead. It starts on applicationDidResignActive and stops on applicationDidBecomeActive (where the page's rAF loop resumes ownership). Native timers keep firing in the background (active audio playback prevents App Nap), so the override stays installed and media keys continue to advance the native queue. Verified at runtime: 5 next presses over 60+s backgrounded each routed through REMOTE_NEXT -> next() and advanced to the following queue track, with zero drift-recovery restarts. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Owner
Author
|
Superseded by upstream PR sozercan#319. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Pressing the macOS media next key while the app is in the background sometimes restarts the same song from the beginning instead of advancing to the next track in the queue.
Root cause
In
nextPreviousTrackmode, Kaset overrides the WebView'snavigator.mediaSessionnexttrack/previoustrackhandlers so media keys advance the native queue (via theREMOTE_NEXTmessage). YouTube Music periodically re-registers its own handlers, and Kaset re-asserts its override through arequestAnimationFrameloop on the page.WebKit freezes
requestAnimationFramewhile the app is backgrounded, so the override was never re-applied. After some time backgrounded, a media-key press fell through to YouTube's own handler, which jumped to YouTube's recommended video. Queue-drift recovery (handleUnexpectedQueueDriftIfNeeded) then saw a video that wasn't in the native queue and force-reloaded the current queue song from 0 — the same song appeared to restart.Fix
Drive the override re-assertion from a native run-loop
Timerinstead of relying solely on the page'srequestAnimationFrameloop:applicationDidResignActive.applicationDidBecomeActive(where the page's rAF loop resumes ownership), plus one immediate re-assert.Native timers keep firing in the background (active audio playback prevents App Nap), so the override stays installed and media keys continue to advance the native queue. This extends the existing override-maintenance mechanism (
__kasetRefreshMediaControlStyle) rather than introducing a new path.Verification
Confirmed at runtime with a file tracer (since removed): 5
nextpresses over 60+ seconds backgrounded each routed throughREMOTE_NEXT→next()and advanced to the following queue track, with zero drift-recovery restarts.🤖 Generated with Claude Code