# Audio Player

## Objective

Play audio files in place, with a scrubber, volume, and an honest elapsed time.

A compact inline audio player attached to every audio file the app already stores or links to.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Build one player component and use it everywhere audio appears. If the app already has Adaptive Media Source Component or a video player, extend that shared media layer rather than standing up a second, differently-behaved control.
2. Cover the controls people expect: play and pause, a draggable scrub bar with elapsed and total time, volume with a mute toggle, and a playback speed control if the audio is spoken word.
3. Make the whole player keyboard operable. Space toggles playback, arrow keys seek and change volume, and every control has a real accessible name rather than an unlabelled icon.
4. Serve the audio through the app's existing file access rules. A player embedded on a private record must not expose a media URL that plays for anyone who has the link.
5. Do not let two players sound at once. When one starts, pause every other player on the page rather than letting the user hunt for whichever one is still talking.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- A visitor who starts a track and then navigates within the app expects it to keep playing. Either hoist the player above the routed view so it survives navigation, or decide deliberately that it stops and make that stop clean rather than abrupt.
- Buffering and paused look identical if you only toggle a play icon. Show a distinct loading state on the transport so a stalled network does not read as a broken player.
- Some files never report a duration, either because the server does not send a length or because the stream is open-ended. Show elapsed time and a working scrubber that degrades to an unknown-length state instead of rendering a total of zero.
- On mobile the player should register its metadata with the operating system so the lock screen and hardware media keys show the title and control playback, rather than leaving the user with a silent, unlabelled notification.
- Browsers block autoplay with sound. A player that starts muted to dodge the block and then unmutes itself is worse than one that waits for a tap.
- Seeking before the file has buffered that far must not reset playback to the start or throw the player into a permanent loading state.
- A file that fails to load, is the wrong type, or has been deleted must show an error with the filename and a download link, not an inert transport with a dead play button.
- The scrub bar needs a hit area large enough for a thumb, and dragging it must not select the surrounding page text.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Every audio file in the app plays through the same player component.
- [ ] Play, pause, seek, and volume are all reachable by keyboard and correctly labelled for screen readers.
- [ ] Starting one player pauses any other player on the page.
- [ ] Buffering is visually distinct from paused, and files with no duration metadata still play and seek.
- [ ] Media keys and lock-screen metadata work on mobile.
- [ ] Audio attached to private records is not playable without the same permission the record requires.
- [ ] A missing or unplayable file shows an error state with a fallback download.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
