The Audio()
constructor creates and returns a new HTMLAudioElement
which can be either attached to a document for the user to interact with and/or listen to, or can be used offscreen to manage and play audio.
The Audio()
constructor creates and returns a new HTMLAudioElement
which can be either attached to a document for the user to interact with and/or listen to, or can be used offscreen to manage and play audio.
js
new Audio() new Audio(url)
url
Optional
An optional string containing the URL of an audio file to be associated with the new audio element.
A new HTMLAudioElement
object, configured to be used for playing back the audio from the file specified by url
. The new object's preload
property is set to auto
and its src
property is set to the specified URL or null
if no URL is given. If a URL is specified, the browser begins to asynchronously load the media resource before returning the new object.
You can also use other element-creation methods, such as the document
object's createElement()
method, to construct a new HTMLAudioElement
.
There are three ways you can tell when enough of the audio file has loaded to allow playback to begin:
readyState
property. If it's HTMLMediaElement.HAVE_FUTURE_DATA
, there's enough data available to begin playback and play for at least a short time. If it's HTMLMediaElement.HAVE_ENOUGH_DATA
, then there's enough data available that, given the current download rate, you should be able to play the audio through to the end without interruption. canplay
event. It is sent to the <audio>
element when there's enough audio available to begin playback, although interruptions may occur. canplaythrough
event. It is sent when it's estimated that the audio should be able to play to the end without interruption. The event-based approach is best:
js
myAudioElement.addEventListener("canplaythrough", (event) => { /* the audio is now playable; play it if permissions allow */ myAudioElement.play(); });
If all references to an audio element created using the Audio()
constructor are deleted, the element itself won't be removed from memory by the JavaScript runtime's garbage collection mechanism if playback is currently underway. Instead, the audio will keep playing and the object will remain in memory until playback ends. At that time, the object becomes subject to garbage collection.
Specification |
---|
HTML Standard # dom-audio-dev |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
Audio |
4 | 12 | 3.5 | 9 | ≤12.1 | 3.1 | ≤37 | 18 | 4 | ≤12.1 | 3 | 1.0 |
<audio>
.
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement/Audio