Nos bénévoles n'ont pas encore traduit cet article en Français. Inscrivez-vous et aidez-nous à réaliser cette tâche !
Vous pouvez également lire cet article en English (US).

The HTMLAudioElement interface provides access to the properties of <audio> elements, as well as methods to manipulate them. It derives from the HTMLMediaElement interface.

Properties

No specific properties; inherits properties from its parent, HTMLMediaElement, and from HTMLElement.

Methods

Inherits methods from its parent, HTMLMediaElement, and from HTMLElement.

Constructor

Syntax

mySound = new Audio([URLString]);

Description

Constructor for audio elements. The preload property of the returned object is set to auto and the src property is set to the argument value. The browser begins asynchronously selecting the resource before returning the object.

Note: An Audio element constructed with new Audio() won't be garbage collected as long as playback is in progress. It will continue playing and be audible until pause() is called or playback finishes.

Parameters

URLString (expected type: DOMString; optional)
The src property of the constructed HTMLAudioElement.

Regular methods

Name & Arguments Return Description
mozCurrentSampleOffset() unsigned long long Indicates the current offset of the audio stream that was created by a call to mozWriteAudio(). This offset is specified as the number of samples since the beginning of the stream.

mozSetup(in PRUint32 channels, in PRUint32 rate)

void Sets up the audio stream for writing. You specify as parameters the number of channels (1 for mono or 2 for stereo), then the sampling rate (44100 for 44.1kHz, for example).
mozWriteAudio(in jsval data) unsigned long Writes audio into the stream at the current offset. Returns the number of bytes actually written to the stream.

Examples

Basic usage

You can create your HTMLAudioElement entirely with JavaScript:

var horn = new Audio('car_horn.wav');
horn.play();

Some of the more commonly used properties of the audio element includes src, currentTime, duration, paused, muted and volume. This snippet copies the audio file's duration to a variable:

var horn = new Audio('car_horn.wav');
horn.addEventListener('loadeddata', () => {
  let duration = horn.duration;
  // The duration variable now holds the duration (in seconds) of the audio clip 
})

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'HTMLAudioElement' in that specification.
Living Standard  
HTML5
The definition of 'HTMLAudioElement' in that specification.
Recommendation  

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support YesEdge Full support YesFirefox Full support 3.5IE Full support 9Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
mozCurrentSampleOffset
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox Full support 4IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android Full support YesOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
mozSetup
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox Full support 4IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android Full support YesOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
mozWriteAudio
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox Full support 4IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android Full support YesOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No

Legend

Full support  
Full support
No support  
No support
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.

See also

  • HTML element implementing this interface: <audio>.

Étiquettes et contributeurs liés au document

Dernière mise à jour par : mfluehr,