Skip to content

Fix audio track selection: manual picks first, automatic correction second - #1003

Merged
Riteshp2001 merged 3 commits into
Riteshp2001:masterfrom
fzs209:pr/audio-manual-selection
Nov 19, 2026
Merged

Riteshp2001 merged 3 commits into
Riteshp2001:masterfrom
fzs209:pr/audio-manual-selection

Conversation

@fzs209

@fzs209 fzs209 commented Nov 19, 2026

Copy link
Copy Markdown

Fixes #1001

Problem
A hand-picked audio track was not treated as a user choice. The restored track was applied on the next open, but the automatic language-based correction then ran anyway and overrode it within a fraction of a second. A selection the app itself had made automatically was also saved with the same weight as a manual pick, and restores were skipped entirely for opens that carried a start position.

Fix — decision order, applied identically on the foreground and detached (background service) paths

  1. When mpv.conf owns alang, the app does not intervene at all: no manual restore, no automatic correction, and no manual record is written.
  2. A saved manual selection decides the load. It is applied inside the playback-state restore, before the position restore completes and before any automatic step — on every load, including loads that carry a start position. If the saved track no longer exists (for example an external audio file that is missing), only that restore is skipped; the record is kept, and no wrong track is substituted.
  3. The automatic correction runs only when no manual record decides the load.
  4. Picks the app makes itself (automatic correction, clean fallback) are never recorded as manual.

Implementation

  • The playback state now records whether the audio selection was manual, which track it was (with "off" as an explicit value), and whether it was an external file. Database version 31 → 34, with migrations that accept databases starting at 31, 32 or 33; rows written by older versions count as "not manual".
  • Manual picks are marked only from actual manual track selections. The mark carries the load generation, and the correction gate for a load only honours a mark made for that same load — a mark left over from an earlier load in the same process no longer suppresses the correction.
  • When validating a saved selection, only audio tracks are searched (a saved audio id of 1 previously collided with the video track and was always rejected).
  • On the detached path, a saved external audio file is re-added first, then the saved manual selection is applied; if there is none, the automatic correction runs as before.

Testing
These are the same three commits that run on my fork's fongmi-fixes branch, cherry-picked onto current master (bd4ac09) with no conflicts. On the fork build I verified on a real device: manual picks restored in-process and in fresh processes, for embedded tracks and for external audio files (including an external track landing behind seven embedded ones), and the negative case where the saved external file is missing — playback continued on the core's own pick and the record was kept. A CI build of this exact branch is running here: https://github.com/fzs209/mpvRx/actions/runs/44548906669

Disclosure: the fix and this text were produced with AI assistance; all testing described above was performed by me personally on the real device.

…econd

Decision order, applied identically on the foreground and detached paths:

1. When mpv.conf owns alang, the app does not intervene at all: no manual
   restore, no automatic correction, and no manual record is written.
2. A saved manual selection decides the load. It is applied inside the
   playback-state restore (before the position restore completes and
   before any automatic step), on every load including loads that carry
   a starting position, and it covers both a picked track and audio
   turned off by hand. External audio tracks are re-added first, because
   a manual pick can point at one of them.
3. With no manual record, mpv picks by preferred language (alang) as
   before.
4. The automatic correction (TrackSelector) only ever adjusts step 3's
   result, and never runs when step 2 decided: it keeps mpv's track when
   it is a clean track (no commentary/description markers, plural forms
   included) in any preferred language; otherwise it chooses among clean
   tracks by preference order with the file default track winning ties;
   otherwise it falls back to a clean track so a commentary track or a
   missing selection is not left in place.

What was wrong and how this fixes it:

- The old restore only ran with no position override, only for aid > 0,
  and conflated "audio off" (-1) with "no record". Manual selections are
  now stored separately (hasManualAudioSelection + manualAudioId, where
  0 is manual off and -1 is no record), so all three states are distinct.
- TrackSelector could override a restored pick with its own preference
  match, and its keep/re-pick rules disagreed with the restore. It now
  skips the audio step entirely when the restore decided, and its
  correction follows the single order above.
- Every save stored whatever track was playing as the file's selection.
  Now only an explicit pick (PlayerViewModel.selectAudioTrack) creates a
  manual record, via ManualAudioSelectionTracker binding the exact track
  id; automatic changes (mpv's choice, the correction, quality-change
  pairing) are never persisted as manual, and a save without a pick
  carries the stored record over untouched. Both save sites
  (PlayerActivity, MediaPlaybackService) use the same rule.
- A saved id that is unusable for a load (file replaced, id now belongs
  to another track type) is "not applicable this time", not "record
  void": validation reuses the track list TrackSelector already reads,
  the automatic rules decide that open, and the record stays stored.
- External audio tracks were not persisted at all. They are now stored
  like external subtitles (externalAudio), tracked per load generation
  in PlayerViewModel (not cleared by title, which repeats across files),
  and re-added unselected before the manual restore validates.
- Detached loads driven by MediaPlaybackService had no restore path at
  all. The service now runs the same sequence per load generation while
  it owns playback: re-add external audio, apply the manual selection,
  then the automatic correction.
- A yt-dlp audio pick reloads the item and renumbers its tracks, so the
  pick is recorded as reload-pending and resolved at restore time to the
  track the reload selected.

Database 31 -> 34 (34 stays above the two reverted builds' 32/33):
31 -> 34 adds the three columns with "no manual record" defaults, so
pre-fix rows are decided by the automatic rules, per the migration
policy that old aid values are not manual picks. 32 -> 34 clears the
interim build's flags (that build marked every row manual) instead of
carrying them over. 33 -> 34 carries the reverted build's flagged picks
from aid into manualAudioId. Room schema JSON is generated at build
time, as in the previous audio-fix attempts.
applySavedAudioSelection validated the saved track id against the whole
track list. mpv track ids are per type - video, audio and subtitles each
number from 1 - and the video track comes first in the list, so a saved
aid of 1 matched the video track, failed the type check, and the manual
record was reported "not usable" on every load. The automatic fallback
then decided instead - or, when the gate was closed by an in-process
mark, nothing decided and the open stayed silent.

Validate inside the audio tracks only: the first track that is both an
audio track and carries the saved id. No new reads: the validation uses
the track list this step already fetched.
The gate after the foreground restore treated any in-process manual-pick
mark for the file as "audio already decided". Marks live for the whole
process and are never cleared, so a pick made in an earlier open of the
same file suppressed the automatic correction in every later open - even
when that open's restore could not apply the saved record. Both paths
then decided nothing and the open could stay silent.

A mark now records the load generation in which the pick was made, and
the gate only counts a mark from the load it is deciding for. That keeps
the case the mark term exists for - a pick made during this load, while
its restore and subtitle autoload were still running, which the
correction must not override - while a pick from an earlier load no
longer closes the gate. Picks still carry across loads through the
persisted record and the restore, unchanged; the save path reads the
marked track id exactly as before.
@vercel

vercel Bot commented Nov 19, 2026

Copy link
Copy Markdown

@fzs209 is attempting to deploy a commit to the riteshp2001's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Riteshp2001
Riteshp2001 merged commit 9aac7b0 into Riteshp2001:master Nov 19, 2026
5 of 6 checks passed
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.

[Bug] A hand-picked audio track is not treated as a user choice: on the next open, the automatic selection overrides the restored track

2 participants