Tutorial·8 min read

How to Capture System Audio in the Browser

A comprehensive guide to capturing system audio using the Web Audio API and getDisplayMedia. Learn how browser-based audio capture works and how to use it for real-time visualization.

Understanding Browser Audio Capture

Capturing system audio in a web browser has become possible thanks to the Screen Capture API, specifically the getDisplayMedia method. Unlike microphone input (getUserMedia), getDisplayMedia allows you to capture the audio output of an entire tab, window, or screen — making it perfect for music visualization.

The Technical Foundation

Browser audio capture relies on two key APIs working together:

  • Screen Capture API (getDisplayMedia): Initiates a screen or tab share and can include the audio track from that source.
  • Web Audio API: Processes the captured audio stream in real time, providing frequency analysis, waveform data, and more.
  • Step-by-Step Implementation

    #### 1. Request Audio Capture

    The first step is to call getDisplayMedia with audio enabled:

    const stream = await navigator.mediaDevices.getDisplayMedia({
      video: true,  // Required by the API
      audio: true,  // Enables audio capture
    });
    

    Note that video: true is required by the spec even if you only want audio. You can immediately stop the video track after receiving the stream to reduce resource usage.

    #### 2. Extract the Audio Track

    Once you have the stream, verify that an audio track is present:

    const audioTracks = stream.getAudioTracks();
    if (audioTracks.length === 0) {
      throw new Error('No audio track — the user may not have checked "Share audio"');
    }
    

    #### 3. Connect to the Web Audio API

    Create an AudioContext and connect the stream:

    const audioContext = new AudioContext();
    const source = audioContext.createMediaStreamSource(stream);
    const analyser = audioContext.createAnalyser();
    analyser.fftSize = 1024;
    source.connect(analyser);
    

    #### 4. Read Frequency Data

    In your animation loop, extract real-time audio data:

    const frequencyData = new Uint8Array(analyser.frequencyBinCount);
    analyser.getByteFrequencyData(frequencyData);
    

    Browser Support

  • Chrome / Edge: Full support for tab and screen audio capture.
  • Firefox: Partial support — tab audio works, system audio varies by OS.
  • Safari: Limited support — audio capture from tabs is available in recent versions.
  • Common Pitfalls

  • User must explicitly enable audio sharing in the screen share dialog. There is no way to force this programmatically.
  • CORS restrictions may affect audio from cross-origin iframes.
  • Some content uses DRM or Encrypted Media Extensions that block audio capture.
  • Best Practices

  • Always stop the video track immediately if you only need audio.
  • Use smoothingTimeConstant on the AnalyserNode to reduce jitter.
  • Handle the case where the user stops sharing mid-session.
  • Provide clear instructions to the user about enabling audio in the share dialog.
  • System audio capture opens up exciting possibilities for browser-based music visualization, audio analysis tools, and interactive experiences — all without requiring any downloads or plugins.

    Last updated: March 10, 2026

    More from the Blog