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
Fundamentals
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.
Concept
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.
Also — setPositionState() for duration / position progress (where supported).
Baseline — Widely available for the property (MDN, since about September 2021).
Foundation
📝 Syntax
General form of the property:
JavaScript
navigator.mediaSession
Value
A MediaSession object used to share playback metadata and handle media control actions.
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");
}
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.
UniversalWidely available
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
mediaSessionExcellent
Bottom line: Detect mediaSession, set MediaMetadata + playbackState, register action handlers, and keep them synced with your player.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about mediaSession
Baseline Media Session entry — metadata, playback state, and media-key handlers.
5
Core concepts
🎧01
Returns
MediaSession
Entry
📚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.