Audio() - Web APIs 编辑

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.

Syntax

audioObj = new Audio(url);

Parameters

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

Return value

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.

    Usage notes

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

    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.

    Specifications

    SpecificationStatusComment
    HTML Living Standard
    The definition of 'Audio()' in that specification.
    Living Standard

    Browser compatibility

    BCD tables only load in the browser

    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.

    See also

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
    列表为空,暂无数据

    词条统计

    浏览:125 次

    字数:5706

    最后编辑:7 年前

    编辑次数:0 次

      我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
      原文