Skip to content

YouTube: resume the last video after the app is restarted - #856

Open
sheedonz wants to merge 4 commits into
AndreyPavlenko:masterfrom
sheedonz:youtube-resume
Open

sheedonz wants to merge 4 commits into
AndreyPavlenko:masterfrom
sheedonz:youtube-resume

Conversation

@sheedonz

@sheedonz sheedonz commented Nov 4, 2026

Copy link
Copy Markdown

Problem

When the car is switched off, Android Auto disconnects and the Fermata process is usually killed. Next time Fermata is opened, YouTube always starts on the home page and the unfinished video is lost.

YoutubeFragment only keeps the page URL in the fragment's saved state (onSaveInstanceState), which doesn't survive the process being killed. YT_RESUME_POS is persisted, but it's only applied when the YouTube engine is already the active one, which is never the case after a fresh start.

Change

  • Save while playing: the URL and position of the playing video are stored in the addon preferences every 5 seconds and on pause.
    • The URL comes from the player (movie_player.getVideoData().video_id) when the page isn't a /watch or /shorts/ page. m.youtube.com plays search results and feed items inline without changing location.href, so the page URL alone isn't enough.
  • Restore on a fresh start: when the fragment is created without saved state, the saved video is loaded with t=<seconds>s.
  • Unmute once: YouTube mutes videos that autoplay without a user gesture, so the resumed video is unmuted once when it starts playing. Other autoplayed videos (feed previews) are left alone.
  • Clear when finished: the saved video is removed when it ends.
  • New setting: "Resume last video" in the YouTube addon settings, enabled by default.

Restarts where the fragment's saved state survives are unchanged.

Testing

Tested on an Honor 400 Pro (Android 16) with a 2.0.2 auto build that includes this change:

  • Played a video from search results (inline playback), force-stopped the app at 35 s, relaunched: it opened the same video at 35 s and kept playing. Repeated at 75 s and 235 s with the same result.
  • The resumed video plays with sound (no "tap to unmute"; an active media audio player is reported).
  • The new setting appears in Settings → Addons → YouTube.

Not tested: inside a real car (my own build isn't listed by Android Auto because it's not signed with your key, which is why I'm sending this upstream rather than running a fork), and the "video ended clears the session" path.

Only English strings are added; the other values-* files fall back to them.

🤖 Generated with Claude Code

The video URL was only kept in the activity's saved state, so it was lost
when the process was killed, e.g. when Android Auto disconnects because
the car is switched off. YouTube then always opened the home page.

- Save the playing video's URL and position every 5 s and on pause. Use
  the player's video id, since search results and the feed play videos
  inline without changing the page URL.
- On a fresh start, open the saved video at the saved position (t=) and
  unmute it once, because YouTube mutes videos that autoplay without a tap.
- Clear the saved video when it ends.
- New "Resume last video" setting, enabled by default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rickselest

rickselest commented Nov 5, 2026 •

Copy link
Copy Markdown

This sounds awesome. Would love this feature. Thanks for working on it.

@AndreyPavlenko AndreyPavlenko left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: resume last video. Request changes on the must-fix items below. Should-fix items can land in this PR.

Must fix

  1. Autoplay previews overwrite and clear the saved session. JS_VIDEO_ENDED calls clearLastVideo() unconditionally, so a finished feed or search preview deletes the user's unfinished video. timeupdate and pause also save for any playing video, so the stored target becomes whatever played last. Suggested fix: skip progress saves while the video is muted, and on ended clear only when the ended video id matches the saved id (send the id with the event).

  2. The unmute flag can be consumed by the wrong video. unmuteOnPlay() is armed in onViewCreated before loadUrl(DEFAULT_URL). If the home page autoplays a muted preview, the first playing event consumes the flag: the preview is unmuted and the resumed video stays muted. Suggested fix: pass the resumed video id to unmuteOnPlay(id) and unmute in playing() only when fermataVideoUrl() contains that id. Note that fix 1 and this one interact: the resumed video starts muted, so the "skip while muted" check must exempt it.

Should fix

  1. Progress is saved even when "Resume last video" is off. saveLastVideo writes prefs every 5 s regardless of YT_RESUME_LAST. Gate the save on the pref.

  2. lastSavedUrl only drives logging. Remove the field and the changed flag, or drop the log lines.

  3. getVideoUrl() fallback conflicts with isVideoUrl. Stream URLs usually lack /watch? and /shorts/, so the filter rejects them. Drop the branch or make the filter match what it returns.

  4. No automated check for the new logic. getResumeUrl (query rewrite, t replacement, pos > 0) and saveProgress parsing are plain Java and easy to unit test. The ended path is untested per the description.

Also consider: the p.unMute() plus v.muted = false pair is a workaround for platform muting; a comment saying so would help the next reader.

— Claude

sheedonz and others added 3 commits November 10, 2026 17:43
…are URL helpers

- Restore YT_PLAYER_CONTROLS, YT_PAUSE_ON_LEAVE and YT_REOPEN_VIDEO_MIN,
  which the merge dropped
- Save the last video only when "Resume last video" is enabled
- On ENDED, clear the saved video only if it is the one that ended
- Move URL/progress parsing into YoutubeUrls (no android.net.Uri)
- Fix YoutubeFragment.onViewCreated, which the merge left mangled

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…muted progress, share URL helpers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sheedonz sheedonz closed this Nov 10, 2026
@sheedonz sheedonz reopened this Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants