This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.
The HTMLMediaElement.play()
method attempts to begin playback of the media and returns a Promise
which is fulfilled when the playback has been successfully started, and which is rejected if playback fails to begin for any reason (such as permission issues or other problems).
Syntax
var Promise = HTMLMediaElement.play();
Parameters
None.
Return value
A Promise
which is fulfilled when playback has been started, or is rejected if for any reason playback cannot be started.
Exceptions
The promise's rejection handler is called with an exception name passed in as its sole input parameter (as opposed to a traditional exception being thrown). Possible exceptions include:
NotAllowedError
- The user agent (browser) or operating system doesn't allow playback of media in the current context or situation. This may happen, for example, if the browser requires the user to explicitly start media playback by clicking a "play" button.
NotSupportedError
- The media source (which may be specified as a
MediaStream
,MediaSource
,Blob
, orFile
, for example) doesn't represent a supported media format.
Other exceptions may be reported, depending on browser implementation details, media player implementation, and so forth.
Example
This example starts playing a video element, updating a status box with a celebratory message if the playback is started successfully or with an error message if an error occurs.
let myVideo = document.getElementById("myVideoElement"); myVideo.play().then(() => { document.getElementById("statusText").innerHTML = "Yay! Video is playing!"; }).catch((error) => { document.getElementById("statusText").innerHTML = "Error: " + error; });
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'play()' in that specification. |
Living Standard | Initial definition; living specification. |
HTML5 The definition of 'play()' in that specification. |
Recommendation | Initial definition. |
Note that the WHATWG and W3C versions of the specification differ (as of April 20, 2016) as to whether or not this method returns a Promise
or nothing at all, respectively.
Browser compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 50.0 | (Yes) | 3.5 (1.9.1)[1] | ? | ? | ? |
Promise return value | 50.0 | ? | 53 (53) | ? | ? | ? |
Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|---|
Basic support | No support | 50.0 | (Yes) | (Yes) | ? | ? | ? | ? | 50.0 |
Promise return value | CompatUnknown | CompatUnknown | CompatUnknown | 53.0 (53) | CompatUnknown | CompatUnknown | CompatUnknown | CompatUnknown | CompatUnknown |