W3cubDocs

/DOM

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) => {
 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 No support 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 ? ? ? 53.0 (53) ? ? ? ? ?

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/play