DOM Methods

HTMLMediaElement.play()

Requests playback. Its promise fulfills once playback starts, or rejects if playback is blocked or the source is unsupported. Update UI after fulfillment, not merely after calling play. This example generates a muted one-second local WAV. Example result: true.

Formal Syntax & Structure

html syntaxhtml
media.play()

Return Value

Promise<void>

Resolves when playback starts.

Working Example

Example Snippethtml
<p>  Result:  <output id="result"></output></p><script>  const result = document.getElementById("result");  (async () => {  const media = document.createElement("audio"); document.body.append(media);  const bytes = new Uint8Array(8044); const view = new DataView(bytes.buffer); for (const [offset,text] of [[0,"RIFF"],[8,"WAVE"],[12,"fmt "],[36,"data"]]) { for (let j=0;j<text.length;j++) bytes[offset+j]=text.charCodeAt(j); } view.setUint32(4,8036,true); view.setUint32(16,16,true); view.setUint16(20,1,true); view.setUint16(22,1,true); view.setUint32(24,8000,true); view.setUint32(28,8000,true); view.setUint16(32,1,true); view.setUint16(34,8,true); view.setUint32(40,8000,true); bytes.fill(128,44); const url=URL.createObjectURL(new Blob([bytes],{type:"audio/wav"})); media.src=url;  media.muted = true; await media.play(); result.textContent = !media.paused; media.pause(); URL.revokeObjectURL(url);  })().catch(error => { result.textContent = "Error: " + error.name; });</script>
HTML Reference