Audio()

翻译不完整。 请帮助我们翻译这篇文章!

Audio() 构造器创建并返回一个 HTMLAudioElement,可以将它附加到文档中以供用户交互,也可以用于管理和播放背景音乐。

语法

audioObj = new Audio(url);

参数

url 可选
An optional DOMString containing the URL of an audio file to be associated with the new audio element.

返回值

新创建的 HTMLAudioElement 对象, 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.

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();
});

内存使用与管理

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 Living Standard
Audio()
Living Standard

浏览器兼容性

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.

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Audio() constructorChrome Full support YesEdge Full support 12Firefox Full support 3.5IE Full support 10Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support

参见