<div align="center">

# ⚡ Spark Player

### A dependency-free HTML5 video player with a smart, paginated playlist.

One player for **MP4, WebM, HLS, YouTube, Vimeo, Dailymotion and Facebook** — a consistent API,
six themes, captions, chapters, picture-in-picture and 27 typed events. **Zero runtime dependencies.**

![npm version](https://img.shields.io/npm/v/spark-player.svg)
![license](https://img.shields.io/badge/license-MIT-blue.svg)
![runtime deps](https://img.shields.io/badge/runtime%20deps-0-brightgreen.svg)
![types](https://img.shields.io/badge/TypeScript-ready-3178c6.svg)
![themes](https://img.shields.io/badge/themes-6-8b5cf6.svg)
![events](https://img.shields.io/badge/typed%20events-25-6ee7ff.svg)

[**Live demo**](index.html) · [**API documentation**](documentation.html) · [Changelog](CHANGELOG.md)

</div>

---

## Why Spark Player?

- **One player for every source.** Paste a URL and it plays — a local MP4, an HLS stream, or a
  YouTube, Vimeo, Dailymotion or Facebook link. The source kind is auto-detected per item.
- **Smart, paginated playlist.** Pages of 4, 6 or 8 items (or all), autoplay-next, active-item
  tracking and a mobile bottom-sheet layout.
- **Automatic metadata.** Duration and posters are probed for local files and third-party links,
  and you can always override them.
- **Six polished themes.** Glass, Neon, Desert plus three light/alt variants — all driven by CSS
  variables you can override.
- **Professional controls.** Chapter markers, WebVTT captions, thumbnail seek previews, quality
  and speed menus, volume, PiP, fullscreen and next/prev — fully keyboard accessible.
- **Developer friendly.** A tiny imperative API plus **27 typed event hooks**, full TypeScript
  types, a `<spark-player>` custom element and zero-build CDN usage.
- **Tiny and safe.** No runtime dependencies to audit. hls.js is an optional peer, loaded lazily.

## Full parity — and then some

The table compares Spark Player against the original competitor's published feature list
(see [docs/research/COMPETITOR-SOURCE.md](docs/research/COMPETITOR-SOURCE.md)).

| Capability | The competitor | Spark Player |
| --- | :---: | :---: |
| MP4 / WebM playback | ✅ | ✅ |
| HLS (adaptive) | ✅ | ✅ (hls.js optional) |
| YouTube / Vimeo / Dailymotion / Facebook | ✅ | ✅ |
| Smart paginated playlist (4 / 6 / 8) | ✅ | ✅ |
| Autoplay next | ✅ | ✅ |
| Auto metadata probing | ✅ | ✅ |
| Persistent settings (localStorage) | ✅ | ✅ |
| Built-in themes | 3 | **6** |
| Chapters + WebVTT captions | ✅ | ✅ |
| Thumbnail seek previews | ✅ | ✅ |
| Developer API + event hooks | 20+ | **27 typed events** |
| Quality / level menu | — | ✅ |
| Picture-in-picture | — | ✅ |
| Internationalisation | — | ✅ (11 locales + overrides) |
| Resume / saved positions | ✅ | ✅ (`startTime`) |
| Toast notifications | ✅ | ✅ (`toast()`) |
| React wrapper | — | ✅ (`spark-player/react`) |
| WordPress shortcode + block | — | ✅ |
| Full keyboard shortcuts | — | ✅ |
| Typed TypeScript API | — | ✅ |
| Web component + auto-init | — | ✅ |
| Accessibility (ARIA, WCAG AA target) | — | ✅ |
| Zero runtime dependencies | ✅ | ✅ |
| License | Commercial | **MIT** |

## Quick start

### npm

```bash
npm install spark-player
# optional: adaptive HLS in browsers without native support
npm install hls.js
```

```ts
import { SparkPlayer } from 'spark-player';
import 'spark-player/css';

const player = new SparkPlayer('#player', {
  theme: 'glass',
  pageSize: 6,
  autoplayNext: true,
  playlist: [
    { src: 'media/intro.mp4', title: 'Introduction', poster: 'media/intro.jpg', duration: 92 },
    { src: 'https://example.com/stream.m3u8', title: 'Live HLS stream' },
    { src: 'https://www.youtube.com/watch?v=aqz-KE-bpKQ', title: 'YouTube clip' },
  ],
});

player.on('ready', (e) => console.log('ready', e.item.title, e.kind));
```

### Zero-build CDN

```html
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/spark-player/dist/spark-player.css">
<div id="player"></div>
<script type="module">
  import { SparkPlayer } from 'https://cdn.jsdelivr.net/npm/spark-player/dist/spark-player.js';
  new SparkPlayer('#player', { playlist: [{ src: 'video.mp4', title: 'My video' }] });
</script>
```

> ES modules must be served over HTTP(S); opening the file with `file://` will not work.

## Playlist items

Every item only needs a `src`. Everything else is optional and auto-detected or probed.

```ts
const playlist = [
  {
    src: 'media/feature.mp4',
    title: 'Feature film',
    author: 'Studio',
    poster: 'media/feature.jpg',
    duration: 734,
    subtitles: [
      { src: 'subs/en.vtt', srclang: 'en', label: 'English', default: true, kind: 'captions' },
      { src: 'subs/zh.vtt', srclang: 'zh', label: '中文', kind: 'captions' },
    ],
    chapters: [
      { start: 0, title: 'Intro' },
      { start: 62, title: 'The setup' },
      { start: 240, title: 'The twist' },
    ],
  },
];
```

### Multiple encodings per item

Browsers built without proprietary codecs (Chromium, some Linux builds) cannot
play H.264. List the same video in several formats with `sources` and the
browser picks the first one it can decode — exactly like `<video><source>`:

```ts
{
  src: 'media/feature.mp4',           // detection, resume key, probe fallback
  sources: [
    { src: 'media/feature.webm', type: 'video/webm; codecs="vp9"' },
    { src: 'media/feature.mp4',  type: 'video/mp4; codecs="avc1.4d401f, mp4a.40.2"' },
  ],
  title: 'Feature film',
}
```

`sources` is optional; items without it use `src` directly. If no candidate can
be decoded the player emits `error` with code `no-source-supported`.

## Themes

`glass` · `neon` · `desert` · `glass-light` · `neon-light` · `midnight`

```ts
player.setTheme('neon');
```

Override any colour through CSS variables on `.spark` (or the `accent` option):

```css
.spark { --spark-accent: #ff5c8a; --spark-radius: 20px; }
```

## Developer API

A complete reference — every option, method, event and CSS variable — lives in
[documentation.html](documentation.html). At a glance:

- **Playback:** `play()`, `pause()`, `togglePlay()`, `seek(t)`, `seekBy(d)`, `setRate(r)`
- **Audio / view:** `setVolume(v)`, `setMuted(m)`, `toggleMuted()`, `toggleFullscreen()`, `togglePip()`
- **Appearance / content:** `setTheme(t)`, `setTextTrack(lang)`, `setQuality(q)`
- **Playlist:** `playIndex(i)`, `next()`, `prev()`, `setPageSize(n)`, `setPlaylist(items)`, `addItem()`, `removeItem()`
- **State:** `getState()`, `getCurrentItem()`, `getPlaylist()`, `getSubtitles()`, `getChapters()`, `getQualities()`
- **Feedback / resume:** `toast(message, kind)`, `startTime` + persisted per-item resume (`resume`, `resumeThreshold`)
- **Events:** `on()`, `once()`, `off()` — 27 typed events including `ready`, `timeupdate`, `seeked`, `buffering`, `ended`, `error`, `themechange` and more
- **Lifecycle:** `ready()`, `destroy()`

### Keyboard shortcuts

| Key | Action |
| --- | --- |
| `Space` / `K` | Play / pause |
| `←` / `→` | Seek back / forward |
| `↑` / `↓` | Volume up / down |
| `M` | Mute / unmute |
| `F` | Fullscreen |
| `N` / `P` | Next / previous item |
| `?` | Toggle the keyboard-shortcuts panel |

When the stage scrolls out of view mid-playback, a floating mini player docks bottom-right with
the title, poster thumbnail, play/pause, back-to-player and close actions.

## Integrations

Copy-paste recipes for **vanilla JS, React, Vue 3, WordPress and the `<spark-player>` web component**
are in [documentation.html#integration](documentation.html#integration) and
[docs/INTEGRATION.md](docs/INTEGRATION.md).

## Project structure

```
src/
  index.ts          entry: exports + custom element + autoInit
  player.ts         the SparkPlayer engine
  types.ts          public, frozen type contracts
  adapters/         native + HLS + YouTube/Vimeo/Dailymotion/Facebook
  ui/               controls surface + i18n (11 locales)
  playlist/         paginated smart playlist
  react/            spark-player/react wrapper (optional React peer)
  styles/           themed CSS (single stylesheet)
  core/             emitter, store, dom, utils
wordpress/          shortcode + Gutenberg block implementation
spark-player.php    WordPress plugin entry point
demo/               demo assets (captions, styles, app logic)
index.html          live demo
documentation.html  full API reference
```

## Development

```bash
npm install
npm run dev        # Vite dev server
npm run typecheck  # tsc --noEmit
npm run build      # emit dist/ (ESM + UMD + CSS + types)
npm test           # Vitest (jsdom)
npm run coverage   # coverage report
```

## Browser support

Modern evergreen browsers — Chrome, Edge, Firefox and Safari, on desktop and mobile. Safari plays
HLS natively; other browsers use the optional `hls.js` peer. Third-party embeds require HTTPS.

## License

MIT © Spark Player contributors. See [LICENSE](LICENSE).
