惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
Cisco Talos Blog
Cisco Talos Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
D
Docker
S
SegmentFault 最新的问题
博客园 - 聂微东
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
GbyAI
GbyAI
L
LangChain Blog
Vercel News
Vercel News
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
Engineering at Meta
Engineering at Meta
T
Threat Research - Cisco Blogs
T
Threatpost
Scott Helme
Scott Helme
T
Tailwind CSS Blog
Latest news
Latest news
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
The Register - Security
The Register - Security
罗磊的独立博客
P
Proofpoint News Feed
腾讯CDC
S
Schneier on Security
雷峰网
雷峰网
A
About on SuperTechFans
T
Tenable Blog
F
Full Disclosure
Cyberwarzone
Cyberwarzone
博客园_首页
有赞技术团队
有赞技术团队
K
Kaspersky official blog

DEV Community

Pick a better video thumbnail automatically with FFmpeg, PySceneDetect, and CLIP How Instagram Stores Reels, Photos, and Drafts Behind the Scenes Building a Reproducible Offline-First Data Sync Engine for Edge Analytics AI Doesn’t Make Us Think Less by Default, But It Makes It Easier to Skip Thinking Why your devcontainer fails on corporate networks (and how to fix it) The Agent That Lives on a $5 VPS — Why Hermes Changes the Open Source AI Story Claude Code: I Had 10 Plugins Active at Once — Here's What It Actually Costs Stop your app from booting with broken env vars: a type-safe, universal config library 🚀 I Built Trade MCP: Remote MCP Server for Crypto Tools and Safer AI Trading Workflows How I Stopped Node.js from Freezing While Bulk-Processing 1,500+ Excel Rows A Beginner’s Guide to Git Branching and Merging (Without the Panic) CTF Lab Writeup: "Bypass Me" — PicoCTF Binary Exploitation Challenge Configure Audit Logging in Kubernetes VARIABLE: Smart Home Devices Are Collecting More Than You Think — Here's What to Do Webtree - Resources Hub for Dev's Using Server-Sent Events (SSE) in Capacitor 8 with Nuxt 4 Temporal Anchoring in Adversarial Networks: The Cryptographic Physics of History AI Is Eating the World Layer by Layer — Here's Where to Stand Stop Fighting Your AI Coding Agent: A Developer's Guide to Thinking in Collaboration, Not Commands One Playwright Selector Trick Nobody Talks About: getByRole The Complete Guide to Resolving Git Merge Conflicts: From Beginner to Pro Stop writing lazy AI prompts: a hotkey that structures them for you I built a visual README editor so developers never have to write markdown from scratch again How I Built RepoSense: A GitHub Intelligence CLI With Coral SQL Frank: your supercharged Laravel Sail alternative From 'How to Test AI Code' to 'What Makes Us Human' AI Assisted Multi-repo Version Control The Discipline of Not Fooling Ourselves: Episode 7 — The Cost of Certainty BoxAgnts Introduction (7) — OpenAI API and Anthropic API Why Context Window Is Not Enough for AI Character Memory "NestJS authentication in 5 minutes" LogoQR: I Spent a Week Making QR Codes That Don't Look Like Prison Barcodes 🤖 The Second Brain 🧠 Playbook 📚 (2026 Edition) HealthHermes: A Private AI Health Companion That Remembers Everything and Runs on Your Own Machine 🚀 Building Tapbite – A Multi-Service Delivery Platform (Part 1) Managing Environment Variables Securely with Keycheck Cursor-Driven Development in FastAPI: Using AI to Generate Type-Safe API Schemas and Catch Contract Breaks Before Deployment How WhatsApp Works Without Internet: Offline Messaging and Sync Explained Meta's AI Pendant: What It Means for Budget Builders How I Built a Permanent Testing Server Using Cloudflare Tunnel Guia definitivo para usar o Claude Code com modelos gratuitos (depois de testar 6 métodos) "I Built a Developer-Only Social Platform — Meet Devand 🛠️" Beyond onlyOwner: Fixing Logic Vulnerabilities in DeFi (A RetoSwap Case Study) Building AshaPulse — An AI-Powered Health Assistant for India's Frontline Warriors Digital clock project pro version The Coordination Tax: Six Years Watching a One-Day Feature Take Four Months วิธีการขอ call sign (สัญญาณเรียกงาน) ของนักวิทยุสมัครเล่นแห่งประเทศไทย ฉบับคนที่หมดอายุนานแล้ว (แบบเกือบจับมือทำ) OpenLiDARViewer: Browser-Native LiDAR Visualization for Real-World Workflows new to dev Recording screen on Linux: the state of things in 2026 Streamlining Your Workflow: GitHub Actions CI/CD Pipeline Best Practices The enterprise AI control that is still missing: code provenance Introducing Destawell — Mobile-First Security Research & Open-Source Tooling Stop Storing Plaintext in Browser Cookies — Use AES-GCM Encryption Instead 🐍 How to Use Open Interpreter for Free — With the Latest Models 103. Agent Memory: Short-Term, Long-Term, and Episodic TinyLoad v7 — VEH page-fault decryption and a fully encrypted overlay, what's new in TinyLoad v7.0, my open-source PE packer for Windows How I sleep at night running agents in YOLO mode What Exactly is "The Cloud"? (Cloud Computing for Beginners☁️) Stop Burning Cash on Long-Context RAG: Ephemeral Prompt Caching with Spring AI and JTokkit The Most Used Technology in the World Has Zero Marketing and Product People How to Compress PDF Files in the Browser (No Server Uploads) The Principle of Least Privilege: Operational Speed's Security Cost Your AI Sucks at Math. Fix It With One Command. How Zone01 Kisumu "Build from Scratch" Approach Transformed Me from a Framework User to a Problem Solver Bringing MongoDB Atlas and Voyage AI to Dify: Build RAG Workflows and Data Agents Without Heavy Glue Code Sass isn't dead, but native CSS just replaced its biggest use case. We can finally write reusable, type-safe functions directly in the browser, with zero build tools. I wrote up a practical guide on Dev.to explaining exactly how native `@function` works. Intel Targets World's First Mass Production of Glass Substrates for AI Chip Packaging Stop Burning Tokens on Chat / Agent Loops — Here's What Actually Works 🔮 Hermes Agent 🤖: A Practical Guide 🔥 — and How It Stacks Up Against OpenClaw & GoClaw 📊 I Built a Free AI Business Manager for Street Vendors in Hindi & English CSS @function CSS @function Agent Payment Stablecoin Fallbacks: Do Not Retry the Changed Quote Daily-summary-agent Opus 4.8 barely moved the leaderboard. It moved the one number that decides if your agents can be trusted. I Built an AI Interview Coach That Turns Any Resume Into a Personalized Prep Package — No API Keys Needed The best Claude Code agents are defined by what they refuse to do I Built a Tiny Skeleton Loader for React Why I Generated Synthetic Patients to Make Identity Matching Better SPIFFE Compliance Deep Dive PostgreSQL 08007 오류 원인과 해결 방법 완벽 가이드 I Was Tired of Writing Daily Standups, So I Built an AI Agent using claude code I got tired of LLM observability tools getting acquired. So I built one that can't be. Oracle ORA-00072 오류 원인과 해결 방법 완벽 가이드 Multi-Agent Negotiation Protocols: How AI Agents Should Bargain for Resources uBlock Origin No Longer Works on Chrome - Here Are the Best Alternatives in 2026 SSH Agent Forwarding vs ProxyJump: Why Agent Forwarding Is Dangerous and What to Use Instead The Best Technology Disappears I Built a Production-Oriented Multi-Provider AI Chatbot in Rust — Here's How Markov Chain Coin Sequence: E[HH] vs E[HTH] Explained LLM Deal Flow Automation in CRM The Do-Over Game: Nash Equilibrium at the Golden Ratio Cash Flow Waterfall Model for LBO Automated Client Reporting The Monty Hall Problem: Why Switching Wins 2/3 of the Time Chat With Your Database Using Natural Language: The Future of Business Analytics Google Apps Script Automation Amoeba Extinction Probability: The Branching Process Solution
Build a custom HLS player in React with hls.js (no wrapper libraries)
Mason K · 2026-05-31 · via DEV Community

Mason K

TL;DR

We'll build a custom HLS player on top of hls.js 1.6.x and React 19 with no wrapper library. Play/pause, seek, volume, quality readout, buffer state, and proper error recovery. End result is ~150 lines and bends to whatever UI you want.

📦 Code: github.com/USER/react-hls-player-demo (replace before publishing)

This is the "stop installing video.js for the third time" tutorial. The wrapper libraries are doing two things you need (the MSE shim and the error-recovery loop) and a lot of things you don't (a giant control bar, a plugin model, a CSS skin). The MSE shim is hls.js. The error-recovery loop is fifteen lines. Everything else can be your own React.

0. Versions

node 22.x
react 19.x
hls.js 1.6.16   # latest stable as of 2026-04-13

Enter fullscreen mode Exit fullscreen mode

The 1.7 alpha is interesting (interstitial-ads improvements landed in canary on 2026-05-16) but not yet ready for production. Stick with 1.6 unless you're testing the next major.

1. Setup

npm create vite@latest react-hls-player-demo -- --template react-ts
cd react-hls-player-demo
npm install
npm install hls.js

Enter fullscreen mode Exit fullscreen mode

For test streams, the Apple HLS test page has a few public manifests. The bipbop ad-stitched stream is the classic one:

https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_fmp4/master.m3u8

Enter fullscreen mode Exit fullscreen mode

2. The bare minimum: a hook that mounts hls.js

// src/useHls.ts
import { useEffect, useRef } from "react";
import Hls from "hls.js";

export function useHls(src: string) {
  const videoRef = useRef<HTMLVideoElement>(null);

  useEffect(() => {
    const video = videoRef.current;
    if (!video) return;

    // Safari (and iOS) plays HLS natively. Hand the URL to the browser.
    if (video.canPlayType("application/vnd.apple.mpegurl")) {
      video.src = src;
      return;
    }

    if (!Hls.isSupported()) return;

    const hls = new Hls({
      enableWorker: true,
      lowLatencyMode: true,
      backBufferLength: 30,
    });
    hls.loadSource(src);
    hls.attachMedia(video);

    return () => {
      hls.destroy();
    };
  }, [src]);

  return videoRef;
}

Enter fullscreen mode Exit fullscreen mode

The cleanup matters. React 19's StrictMode mounts every effect twice in development. Without the hls.destroy(), you leak one Hls instance per remount and your dev server's memory usage will climb. Ask me how I know.

// src/App.tsx
import { useHls } from "./useHls";

const SRC = "https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_fmp4/master.m3u8";

export default function App() {
  const videoRef = useHls(SRC);
  return <video ref={videoRef} controls className="w-full max-w-4xl" />;
}

Enter fullscreen mode Exit fullscreen mode

That plays HLS in every modern browser. Eight lines. The wrapper libraries' main value-add, "plays HLS in Chrome", is now done. Everything else is product.

3. Wiring real controls

Drop controls from <video> and write your own. Each control is a small piece of state synced to the element.

// src/Player.tsx
import { useEffect, useRef, useState } from "react";
import Hls from "hls.js";

export function Player({ src }: { src: string }) {
  const videoRef = useRef<HTMLVideoElement>(null);
  const hlsRef = useRef<Hls | null>(null);
  const [playing, setPlaying] = useState(false);
  const [time, setTime] = useState(0);
  const [duration, setDuration] = useState(0);
  const [level, setLevel] = useState<string>("auto");

  useEffect(() => {
    const v = videoRef.current!;
    if (v.canPlayType("application/vnd.apple.mpegurl")) {
      v.src = src;
    } else if (Hls.isSupported()) {
      const hls = new Hls({ enableWorker: true, lowLatencyMode: true });
      hls.loadSource(src);
      hls.attachMedia(v);
      hls.on(Hls.Events.LEVEL_SWITCHED, (_, d) => {
        const lvl = hls.levels[d.level];
        setLevel(lvl ? `${lvl.height}p` : "auto");
      });
      hlsRef.current = hls;
    }
    return () => { hlsRef.current?.destroy(); hlsRef.current = null; };
  }, [src]);

  return (
    <div className="player">
      <video
        ref={videoRef}
        onPlay={() => setPlaying(true)}
        onPause={() => setPlaying(false)}
        onTimeUpdate={(e) => setTime(e.currentTarget.currentTime)}
        onLoadedMetadata={(e) => setDuration(e.currentTarget.duration)}
      />
      <div className="controls">
        <button onClick={() => {
          const v = videoRef.current!;
          playing ? v.pause() : v.play();
        }}>{playing ? "Pause" : "Play"}</button>

        <input
          type="range"
          min={0}
          max={duration || 0}
          step={0.1}
          value={time}
          onChange={(e) => {
            const v = videoRef.current!;
            v.currentTime = Number(e.target.value);
          }}
        />

        <span>{fmt(time)} / {fmt(duration)}</span>
        <span className="quality">{level}</span>
      </div>
    </div>
  );
}

function fmt(s: number): string {
  if (!isFinite(s)) return "0:00";
  const m = Math.floor(s / 60);
  const ss = Math.floor(s % 60).toString().padStart(2, "0");
  return `${m}:${ss}`;
}

Enter fullscreen mode Exit fullscreen mode

The quality readout is the part wrappers tend to hide. LEVEL_SWITCHED fires every time hls.js picks a new ABR rendition, and hls.levels[level] has the bitrate, resolution, and codec strings. Surfacing "now playing 720p" in the corner of your player is a five-line feature that meaningfully changes how power users perceive your product.

4. Manual quality picker

Some users will want to force a quality, usually because they don't trust ABR or because they're on a metered connection. Wire it like this:

const [levels, setLevels] = useState<Hls["levels"]>([]);

useEffect(() => {
  const hls = hlsRef.current;
  if (!hls) return;
  hls.on(Hls.Events.MANIFEST_PARSED, () => setLevels(hls.levels));
}, []);

// in JSX:
<select
  value={hls.currentLevel}
  onChange={(e) => { hls.currentLevel = Number(e.target.value); }}
>
  <option value={-1}>Auto</option>
  {levels.map((l, i) => (
    <option key={i} value={i}>{l.height}p ({Math.round(l.bitrate / 1000)} kbps)</option>
  ))}
</select>

Enter fullscreen mode Exit fullscreen mode

-1 means "let ABR pick"; any non-negative index pins the player to that level. After a user pins to 480p, the seek bar still works and the rest of ABR's logic still runs underneath, but the renderer stays at 480p.

5. The error recovery loop (do not skip)

This is the single thing the wrappers were doing that you actually need.

hls.on(Hls.Events.ERROR, (_, data) => {
  if (!data.fatal) return;

  if (data.type === Hls.ErrorTypes.NETWORK_ERROR) {
    console.warn("hls: network error, restarting load");
    hls.startLoad();
    return;
  }
  if (data.type === Hls.ErrorTypes.MEDIA_ERROR) {
    console.warn("hls: media error, recovering");
    hls.recoverMediaError();
    return;
  }
  console.error("hls: fatal", data);
  hls.destroy();
});

Enter fullscreen mode Exit fullscreen mode

Three rules:

  1. Ignore non-fatal errors. hls.js retries those itself.
  2. Network errors get a fresh startLoad(). This is what saves a viewer who walked into a tunnel.
  3. Media errors get recoverMediaError(). This rebuilds the MSE source buffers, which is the only thing that fixes "the decoder gave up after a corrupt segment."

Add a tiny rate limiter in production. If recoverMediaError fails twice within a few seconds, destroy and recreate the Hls instance with a fresh source. Otherwise you can wedge yourself into an infinite recovery loop.

6. Buffer indicator

Showing "this video is buffering" properly means listening to the right events, not just paused.

const [stalled, setStalled] = useState(false);

const v = videoRef.current!;
v.addEventListener("waiting", () => setStalled(true));
v.addEventListener("playing", () => setStalled(false));

Enter fullscreen mode Exit fullscreen mode

For the YouTube-style "this much of the video is buffered ahead" bar, use videoElement.buffered:

const buffered = videoRef.current?.buffered;
let aheadPct = 0;
if (buffered && buffered.length > 0 && duration > 0) {
  aheadPct = (buffered.end(buffered.length - 1) / duration) * 100;
}

Enter fullscreen mode Exit fullscreen mode

Render a second <progress> underneath the seek bar at that percentage. Total time invested: ten minutes.

7. Subtitles

hls.js parses WebVTT tracks from the manifest into audioTracks and subtitleTracks. Toggle them like:

hls.subtitleDisplay = true;
hls.subtitleTrack = 0;        // -1 to disable

Enter fullscreen mode Exit fullscreen mode

For language selection, hls.subtitleTracks is an array with name, lang, and default flags.

8. Cleanup checklist

Things people forget that bite later:

Mistake Symptom
No hls.destroy() in unmount Memory leak, Chrome devtools shows climbing JS heap
Forgetting Safari native path App works in Chrome dev, breaks on iPhone in QA
Listening to pause instead of waiting for buffering Players show "buffering" when the user manually pauses
Not handling MEDIA_ERROR Random unrecoverable freezes on bad-CDN nights
Hls.isSupported() === false ignored Old browsers show a broken player with no fallback

What's next

There are three places to take this once it's running:

  • Picture-in-Picture. videoRef.current.requestPictureInPicture(). One line, free feature.
  • MediaSession metadata. Set navigator.mediaSession.metadata so the OS lock screen shows the right title and artwork.
  • Player telemetry. Subscribe to Hls.Events.FRAG_LOADED and send the per-segment timings to your analytics. This is gold for debugging "the video stalled, but only for users in Brazil."

If you're playing assets from a managed video API (Mux, FastPix, Cloudflare Stream, api.video), most of them ship their own custom-element player that wires telemetry for you. Use it if you want the analytics for free. Build your own when the player UX is part of the product.

The 1.7 line of hls.js looks promising for interstitial ads and a cleaner LL-HLS toggle. Worth watching the hls.js releases page for the stable 1.7.0 cut later this year.

Happy playing.