JavaScript Navigator mediaSession Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline

What You’ll Learn

navigator.mediaSession returns a MediaSession object so your site can show “now playing” info and respond to OS / keyboard media keys. Learn MediaMetadata, playbackState, setActionHandler, five examples, and try-it labs.

01

Kind

Read-only property

02

Returns

MediaSession

03

Baseline

Widely available

04

Metadata

MediaMetadata

05

State

playbackState

06

Controls

setActionHandler

Introduction

When you play a podcast or song in the browser, the phone lock screen and laptop media keys should show the title and let users pause without opening the tab.

navigator.mediaSession is how a web app shares that information. You set metadata (title, artist, artwork), declare playbackState ("playing" or "paused", and register action handlers so hardware play/pause buttons control your player.

💡
Enhance the OS media UI

Media Session does not replace your <audio> / <video> element — it connects your existing player to the device’s standard media controls.

Understanding the mediaSession Property

Think of it as a bridge between your web player and the operating system’s “now playing” panel.

  • Value — a MediaSession object for the current document.
  • metadata — assign a MediaMetadata instance (title, artist, album, artwork).
  • playbackState — hint such as "none", "paused", or "playing".
  • setActionHandler(action, handler) — react to play, pause, seek, next/previous track, and more.
  • AlsosetPositionState() for duration / position progress (where supported).
  • Baseline — Widely available for the property (MDN, since about September 2021).

📝 Syntax

General form of the property:

JavaScript
navigator.mediaSession

Value

  • A MediaSession object used to share playback metadata and handle media control actions.

Common patterns

JavaScript
// MDN-style metadata update:
if ("mediaSession" in navigator) {
  navigator.mediaSession.metadata = new MediaMetadata({
    title: "Podcast Episode Title",
    artist: "Podcast Host",
    album: "Podcast Name",
    artwork: [{ src: "podcast.jpg" }]
  });
}

navigator.mediaSession.playbackState = "playing";

⚡ Quick Reference

GoalCode
Get the API objectnavigator.mediaSession
Feature detect"mediaSession" in navigator
Set now-playing infonavigator.mediaSession.metadata = new MediaMetadata({…})
Declare play/pausenavigator.mediaSession.playbackState = "playing"
Handle media keysnavigator.mediaSession.setActionHandler("play", fn)
Status (MDN)Baseline Widely available

🔍 At a Glance

Four facts to remember about navigator.mediaSession.

Returns
MediaSession

API entry

Baseline
widely

Since ~Sep 2021

Share
metadata

Title · art

Keys
actions

setActionHandler

📋 mediaSession vs <audio> / <video>

mediaSessionMedia element
JobOS / lock-screen metadata & media keysActual decode and playback
Plays sound?No — coordinates with your playerYes
User seesNotification / lock screen / keyboard UIYour page UI
Combine?Yes — play with the element; mirror state into mediaSession

Examples Gallery

Examples follow MDN Media Session patterns. Prefer Try It Yourself. Lock-screen artwork appears when real media is playing in a supporting browser.

📚 Getting Started

Detect the API and set podcast-style metadata (MDN sample).

Example 1 — Feature Detection

Check whether navigator.mediaSession exists before using it.

JavaScript
const supported = "mediaSession" in navigator;
console.log(supported
  ? "Media Session API available"
  : "Media Session API missing");
Try It Yourself

How It Works

MDN’s own sample starts with this check so older browsers fail gracefully.

Example 2 — Set MediaMetadata (MDN)

Share title, artist, album, and artwork with the browser / OS.

JavaScript
if ("mediaSession" in navigator) {
  navigator.mediaSession.metadata = new MediaMetadata({
    title: "Podcast Episode Title",
    artist: "Podcast Host",
    album: "Podcast Name",
    artwork: [{ src: "podcast.jpg" }]
  });
  console.log("title: " + navigator.mediaSession.metadata.title);
  console.log("artist: " + navigator.mediaSession.metadata.artist);
} else {
  console.log("Media Session API missing");
}
Try It Yourself

How It Works

Artwork entries can include sizes and type. Provide a few image sizes for crisp lock-screen art.

📈 Practical Patterns

Playback state, media-key handlers, and a reusable setup helper.

Example 3 — Set playbackState

Tell the OS whether media is currently playing or paused.

JavaScript
if ("mediaSession" in navigator) {
  navigator.mediaSession.playbackState = "playing";
  console.log("state: " + navigator.mediaSession.playbackState);
  navigator.mediaSession.playbackState = "paused";
  console.log("state: " + navigator.mediaSession.playbackState);
} else {
  console.log("Media Session API missing");
}
Try It Yourself

How It Works

Keep playbackState in sync with your audio/video element’s play and pause events.

Example 4 — setActionHandler for Play / Pause

Respond when the user presses keyboard or lock-screen media buttons.

JavaScript
if ("mediaSession" in navigator) {
  const log = [];
  navigator.mediaSession.setActionHandler("play", function () {
    log.push("play");
    navigator.mediaSession.playbackState = "playing";
    // audio.play();
  });
  navigator.mediaSession.setActionHandler("pause", function () {
    log.push("pause");
    navigator.mediaSession.playbackState = "paused";
    // audio.pause();
  });
  console.log("handlers registered: play, pause");
  console.log("Press media keys while a track is active to fire them");
} else {
  console.log("Media Session API missing");
}
Try It Yourself

How It Works

Pass null as the handler to clear an action. Common actions include previoustrack, nexttrack, and seek actions.

Example 5 — Now-Playing Setup Helper

Feature-detect, set metadata, playback state, and basic handlers in one place.

JavaScript
function setupMediaSession(info) {
  if (!("mediaSession" in navigator)) {
    return { ok: false, reason: "unsupported" };
  }
  navigator.mediaSession.metadata = new MediaMetadata({
    title: info.title,
    artist: info.artist,
    album: info.album || "",
    artwork: info.artwork || []
  });
  navigator.mediaSession.playbackState = info.playing ? "playing" : "paused";
  navigator.mediaSession.setActionHandler("play", info.onPlay || null);
  navigator.mediaSession.setActionHandler("pause", info.onPause || null);
  return {
    ok: true,
    title: navigator.mediaSession.metadata.title,
    playbackState: navigator.mediaSession.playbackState
  };
}

console.log(JSON.stringify(setupMediaSession({
  title: "Morning Mix",
  artist: "CodeToFun Radio",
  album: "Demo",
  playing: true,
  onPlay: function () {},
  onPause: function () {}
})));
Try It Yourself

How It Works

Call the helper whenever the track changes so lock-screen info stays accurate.

🚀 Common Use Cases

  • Music / podcast players — show title and artwork on lock screens.
  • Keyboard media keys — play, pause, next, and previous from the OS.
  • Background tabs — keep controls usable while the user is in another app.
  • Video sites — mirror episode metadata into the system media UI.
  • Radio / live streams — update metadata as the current show changes.

🧠 How navigator.mediaSession Works

1

Your page plays media

An audio/video element (or Web Audio) starts playback.

Player
2

You publish MediaSession data

Set metadata, playbackState, and action handlers.

Publish
3

OS shows now-playing UI

Lock screen / notification / keyboard controls update.

OS UI
4

User presses media keys

Your handlers pause, seek, or skip tracks.

📝 Notes

  • Baseline Widely available for navigator.mediaSession (MDN, since about September 2021).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Some MediaSession methods (position state, camera/mic session helpers) may vary — feature-detect.
  • Metadata alone does not play audio; keep your media element in sync.
  • Related: mimeTypes, mediaDevices, JavaScript hub.

Universal Browser Support

navigator.mediaSession is Baseline Widely available across modern browsers (MDN: since about September 2021). Always feature-detect, and verify lock-screen / media-key behavior on your target devices.

Baseline · Widely available

Navigator.mediaSession

Share now-playing metadata and handle OS media controls from your web player.

Universal Widely available
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
mediaSession Excellent

Bottom line: Detect mediaSession, set MediaMetadata + playbackState, register action handlers, and keep them synced with your player.

Conclusion

navigator.mediaSession connects your web media to the device’s standard controls. Set MediaMetadata, keep playbackState accurate, and use setActionHandler so play/pause and track skips work from the lock screen and keyboard.

Continue with mimeTypes, mediaDevices, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Feature-detect with "mediaSession" in navigator
  • Update metadata whenever the track changes
  • Sync playbackState with play/pause events
  • Register play and pause handlers at minimum
  • Provide a few artwork sizes for sharp lock-screen art

❌ Don’t

  • Expect metadata alone to start audio
  • Leave stale titles after skipping tracks
  • Ignore unsupported actions — clear or skip them
  • Forget to update state when autoplay pauses
  • Assume every OS shows identical media UI

Key Takeaways

Knowledge Unlocked

Five things to remember about mediaSession

Baseline Media Session entry — metadata, playback state, and media-key handlers.

5
Core concepts
📚 02

Share

MediaMetadata

Now playing
03

State

playbackState

Hint
04

Keys

setActionHandler

Controls
05

Status

Baseline

Wide

❓ Frequently Asked Questions

It is a read-only Navigator property that returns a MediaSession object. Your page can share now-playing metadata (title, artist, artwork) and handle OS or keyboard media controls via setActionHandler().
No. MDN marks the navigator.mediaSession property Baseline Widely available (since about September 2021). It is not Deprecated, Experimental, or Non-standard. Some related MediaSession methods may still vary by browser.
An object you assign to navigator.mediaSession.metadata with fields like title, artist, album, and artwork image URLs so lock screens and notification centers can show your track.
It registers callbacks for actions such as play, pause, previoustrack, nexttrack, seekbackward, and seekforward when the user presses hardware or OS media keys.
The Media Session property itself is widely available in supporting browsers without a special secure-context note on MDN’s property page. Always feature-detect, and pair it with real media playback (often from a secure origin for other APIs).
The navigator.mediaSession property is read-only (you get a MediaSession object). You do write to that object’s fields, such as metadata and playbackState, and call its methods.
Did you know?

MDN’s podcast example is intentionally tiny: feature-detect, then assign new MediaMetadata({ title, artist, album, artwork }). That alone is enough for many lock screens to show your episode art.

Explore mimeTypes Next

Learn the MimeTypeArray and modern hard-coded PDF entries.

mimeTypes →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful