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.
audioObj = new Audio(url);
url
可选
DOMString
containing the URL of an audio file to be associated with the new audio element.
新的
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
及其
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
对象的
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:
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 or is paused (such as by calling
pause()
). At that time, the object becomes subject to garbage collection.
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'Audio()' in that specification. |
实时标准 |
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Audio()
构造函数
|
Chrome Yes | Edge 12 | Firefox 3.5 | IE 10 | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 4 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
<audio>
.
HTMLAudioElement
Audio()
BeforeUnloadEvent
DOMStringMap
ErrorEvent
GlobalEventHandlers
HTMLAnchorElement
HTMLAreaElement
HTMLBRElement
HTMLBaseElement
HTMLBaseFontElement
HTMLBodyElement
HTMLButtonElement
HTMLCanvasElement
HTMLContentElement
HTMLDListElement
HTMLDataElement
HTMLDataListElement
HTMLDialogElement
HTMLDivElement
HTMLDocument
HTMLElement
HTMLEmbedElement
HTMLFieldSetElement
HTMLFormControlsCollection
HTMLFormElement
HTMLFrameSetElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLImageElement
HTMLInputElement
HTMLIsIndexElement
HTMLKeygenElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMetaElement
HTMLMeterElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOptionsCollection
HTMLOutputElement
HTMLParagraphElement
HTMLParamElement
HTMLPictureElement
HTMLPreElement
HTMLProgressElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLShadowElement
HTMLSourceElement
HTMLSpanElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableDataCellElement
HTMLTableElement
HTMLTableHeaderCellElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTemplateElement
HTMLTextAreaElement
HTMLTimeElement
HTMLTitleElement
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement
HashChangeEvent
历史
ImageData
定位
MessageChannel
MessageEvent
MessagePort
Navigator
NavigatorGeolocation
NavigatorID
NavigatorLanguage
NavigatorOnLine
NavigatorPlugins
PageTransitionEvent
Plugin
PluginArray
PopStateEvent
PortCollection
PromiseRejectionEvent
RadioNodeList
Transferable
ValidityState
Window
WindowBase64
WindowEventHandlers
WindowTimers