This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2019.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The setMediaKeys() method of the HTMLMediaElement interface sets the MediaKeys that will be used to decrypt media during playback.
It returns a Promise that fulfils if the new keys are successfully set, or rejects if keys cannot be set.
setMediaKeys(mediaKeys)
mediaKeysA MediaKeys object that the HTMLMediaElement can use for decryption of media data during playback.
A Promise that fulfills with undefined.
The returned promise may reject an error:
InvalidStateError DOMException
Media keys are already in the process of being attached, or the previous keys cannot be removed at the current time (for example, because the particular implementation does not allow removal during playback).
QuotaExceededErrorThe passed keys are already in use by another element, or the browser is unable to use it with this element for other reasons.
NotSupportedError DOMException
The media keys that are currently associated with the media cannot be disassociated, because this is not supported by either the CDM or the browser.
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
setMediaKeys |
42 | 13 | 38 | 29 | 12.1 | 42 | 38 | 29 | 12.2 | 4.0 | 42 | 12.2 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setMediaKeys