HTMLMediaElement.play()

 

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

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, or File, 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) => {
 // An error ocurred or the user agent prevented playback.
 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

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support Yes Yes3.59 Yes Yes
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes Yes Yes Yes Yes Yes ?

Document Tags and Contributors

Last updated by: jpmedley,