Web Audio & MediaStream APIs Basics0%

Web Audio & MediaStream APIs Basics

Beginner12 min read•Updated: Jul 11, 2026
Study Materials

Web Audio & MediaStream APIs Basics in Modern JavaScript

Modern browsers are capable of processing real-time audio synthesis, visual audio frequency analysis, camera streams, and screen recording without external plugins. The Web Audio API and the MediaStream (WebRTC) API provide low-level access to digital signal processing (DSP) and hardware media devices.


1. The Web Audio API: The AudioNode Graph

The Web Audio API operates as an Audio Routing Graph, where modular AudioNode instances are connected together from source to destination:

Output
┌─────────────────────────────────────────────────────────────┐
│ AUDIO ROUTING GRAPH │
├───────────────┬─────────────────────────────┬───────────────┤
│ Audio Source │ Audio Processing Node │ Destination │
│ (Oscillator / │ (GainNode: volume, │ (Speakers / │
│ MP3 Buffer) │ BiquadFilter: equalizer) │ Headphones) │
└───────┬───────┴──────────────┬──────────────┴───────▲───────┘
│ │ │
└──────► .connect() ───┴──────► .connect() ───┘

Synthesizing a Pure Audio Tone:

Visual Architecture & Process Flow

How data and code flow step-by-step

Flowchart
Step 1
// Step 4: Connect Graph: Oscillator
Step 2
Gain
Step 3
Hardware Speakers

2. MediaStream API: Accessing Camera & Microphone

The navigator.mediaDevices.getUserMedia() API requests permission to capture live hardware audio and video:

JavaScript
async function startWebcamPreview(videoElement) {
try {
const stream = await navigator.mediaDevices.getUserMedia({
video: { width: 1280, height: 720, facingMode: 'user' },
audio: true
});
 
// Stream live webcam into HTML5 <video> element
videoElement.srcObject = stream;
videoElement.play();
} catch (err) {
console.error('Camera access denied or device not found:', err.message);
}
}

3. Screen Recording with getDisplayMedia()

Capturing screen video for presentations or bug recordings:

JavaScript
async function captureScreen(videoElement) {
try {
const screenStream = await navigator.mediaDevices.getDisplayMedia({
video: { cursor: 'always' },
audio: false
});
 
videoElement.srcObject = screenStream;
} catch (err) {
console.warn('Screen capture cancelled by user.');
}
}

4. Teardown: Stopping Media Tracks

Leaving webcam or mic tracks running drains battery and keeps the hardware indicator light on. Always stop active tracks:

JavaScript
function stopStream(mediaStream) {
mediaStream.getTracks().forEach(track => {
track.stop(); // Powers down camera/microphone hardware!
});
}

Interactive Quiz

Practice Quiz

Question 1
How does the Web Audio API process audio data?
Question 2
Why does AudioContext often initialize in a "suspended" state in modern browsers?
Question 3
What API requests hardware permission to stream the user's camera and microphone?
Question 4
How do you turn off a user's webcam and shut down its hardware indicator light after streaming?
Question 5
What API allows a web application to prompt the user to share their screen or a specific application window?
Next Lesson

Performance API (performance.mark & measure)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext