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 可选
An optional 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 .

Determining when playback can begin

There are three ways you can tell when enough of the audio file has loaded to allow playback to begin:

  • Check the value of the 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.
  • Listen for the canplay event. It is sent to the <audio> element when there's enough audio available to begin playback, although interruptions may occur).
  • Listen for the 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();
});
					

Memory usage and management

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 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
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

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改:
  1. HTMLAudioElement
  2. 构造函数
    1. Audio()
  3. 继承:
    1. HTMLMediaElement
    2. HTMLElement
    3. 元素
    4. 节点
    5. EventTarget
  4. HTML DOM 相关页面
    1. BeforeUnloadEvent
    2. DOMStringMap
    3. ErrorEvent
    4. GlobalEventHandlers
    5. HTMLAnchorElement
    6. HTMLAreaElement
    7. HTMLBRElement
    8. HTMLBaseElement
    9. HTMLBaseFontElement
    10. HTMLBodyElement
    11. HTMLButtonElement
    12. HTMLCanvasElement
    13. HTMLContentElement
    14. HTMLDListElement
    15. HTMLDataElement
    16. HTMLDataListElement
    17. HTMLDialogElement
    18. HTMLDivElement
    19. HTMLDocument
    20. HTMLElement
    21. HTMLEmbedElement
    22. HTMLFieldSetElement
    23. HTMLFormControlsCollection
    24. HTMLFormElement
    25. HTMLFrameSetElement
    26. HTMLHRElement
    27. HTMLHeadElement
    28. HTMLHeadingElement
    29. HTMLHtmlElement
    30. HTMLIFrameElement
    31. HTMLImageElement
    32. HTMLInputElement
    33. HTMLIsIndexElement
    34. HTMLKeygenElement
    35. HTMLLIElement
    36. HTMLLabelElement
    37. HTMLLegendElement
    38. HTMLLinkElement
    39. HTMLMapElement
    40. HTMLMediaElement
    41. HTMLMetaElement
    42. HTMLMeterElement
    43. HTMLModElement
    44. HTMLOListElement
    45. HTMLObjectElement
    46. HTMLOptGroupElement
    47. HTMLOptionElement
    48. HTMLOptionsCollection
    49. HTMLOutputElement
    50. HTMLParagraphElement
    51. HTMLParamElement
    52. HTMLPictureElement
    53. HTMLPreElement
    54. HTMLProgressElement
    55. HTMLQuoteElement
    56. HTMLScriptElement
    57. HTMLSelectElement
    58. HTMLShadowElement
    59. HTMLSourceElement
    60. HTMLSpanElement
    61. HTMLStyleElement
    62. HTMLTableCaptionElement
    63. HTMLTableCellElement
    64. HTMLTableColElement
    65. HTMLTableDataCellElement
    66. HTMLTableElement
    67. HTMLTableHeaderCellElement
    68. HTMLTableRowElement
    69. HTMLTableSectionElement
    70. HTMLTemplateElement
    71. HTMLTextAreaElement
    72. HTMLTimeElement
    73. HTMLTitleElement
    74. HTMLTrackElement
    75. HTMLUListElement
    76. HTMLUnknownElement
    77. HTMLVideoElement
    78. HashChangeEvent
    79. 历史
    80. ImageData
    81. 定位
    82. MessageChannel
    83. MessageEvent
    84. MessagePort
    85. Navigator
    86. NavigatorGeolocation
    87. NavigatorID
    88. NavigatorLanguage
    89. NavigatorOnLine
    90. NavigatorPlugins
    91. PageTransitionEvent
    92. Plugin
    93. PluginArray
    94. PopStateEvent
    95. PortCollection
    96. PromiseRejectionEvent
    97. RadioNodeList
    98. Transferable
    99. ValidityState
    100. Window
    101. WindowBase64
    102. WindowEventHandlers
    103. WindowTimers