The HTMLMediaElement interface adds to HTMLElement the properties and methods needed to support basic media-related capability that's common to both audio and video. The HTMLVideoElement and HTMLAudioElement elements both inherit this interface.
Properties
Inherits properties from its ancestors, HTMLElement, Element, Node, and EventTarget.
HTMLMediaElement.audioTracks- An
AudioTrackListthat represents the list ofAudioTrackobjects contained in the element. HTMLMediaElement.autoplay- A
Booleanthat reflects theautoplayHTML attribute, indicating whether playback should automatically begin as soon as enough media is available to do so without interruption. HTMLMediaElement.bufferedRead only- Returns a new
TimeRangesobject that indicates the ranges of the media source that the browser has buffered (if any) at the moment thebufferedproperty is accessed. HTMLMediaElement.controller- A
MediaControllerobject that represents the media controller assigned to the element, ornullif none is assigned. HTMLMediaElement.controls- A
Booleanthat reflects thecontrolsHTML attribute, indicating whether user interface items for controlling the resource should be displayed. HTMLMediaElement.crossOrigin- A string indicating the CORS setting for this media element.
HTMLMediaElement.currentSrcRead only- A
DOMStringwith the absolute URL of the chosen media resource. HTMLMediaElement.currentTime- A
doubleindicating the current playback time in seconds. Setting this value seeks the media to the new time. HTMLMediaElement.defaultMuted- A
Booleanthat reflects themutedHTML attribute, which indicates whether the media element's audio output should be muted by default. HTMLMediaElement.defaultPlaybackRate- A
doubleindicating the default playback rate for the media. HTMLMediaElement.durationRead only- Returns a
doubleindicating the length of the media in seconds, or zero if no media data is available. HTMLMediaElement.endedRead only- Returns a
Booleanthat indicates whether the media element has ended playback. HTMLMediaElement.errorRead only- Returns a
MediaErrorobject for the most recent error, ornullif there has not been an error. HTMLMediaElement.initialTimeRead only- Returns a
doublethat indicates the initial playback position in seconds. HTMLMediaElement.loop- A
Booleanthat reflects theloopHTML attribute, which indicates whether the media element should start over when it reaches the end. HTMLMediaElement.mediaGroup- A
DOMStringthat reflects themediagroupHTML attribute, which indicates the name of the group of elements it belongs to. A group of media elements shares a commoncontroller. HTMLMediaElement.mediaKeysRead only- Returns a reference to the
MediaKeysinterface, which is a set of keys that an associated HTMLMediaElement can use for decription of media data during playback. HTMLMediaElement.mozAudioCaptured- [enter description]
HTMLMediaElement.mozCaptureStream- [enter description]
HTMLMediaElement.mozCaptureStreamUntilEnded- [enter description]
HTMLMediaElement.mozAudioChannelType- Returns a
DOMStringthat can be used to set the audio channel that the sound coming from an<audio>or<video>element will play in, on a Firefox OS device. See Using the AudioChannels API for more details. HTMLMediaElement.mozChannelsRead only- The number of channels in the audio resource (e.g.,
2for stereo). HTMLMediaElement.mozFragmentEnd- [enter description]
HTMLMediaElement.mozFrameBufferLength-
Indicates the number of samples that will be returned in the framebuffer of each
MozAudioAvailableevent. This number is a total for all channels, and by default is set to be the number of channels * 1024 (e.g., 2 channels * 1024 samples = 2048 total).The
mozFrameBufferLengthproperty can be set to a new value, for lower latency, or larger amounts of data, etc. The size given must be a number between 512 and 16384. Using any other size results in an exception being thrown. The best time to set a new length is after theloadedmetadataevent fires, when the audio info is known, but before the audio has started orMozAudioAvailableevents have begun firing. HTMLMediaElement.mozGetMetadata- [enter description]
HTMLMediaElement.mozPreservesPitch- [enter description]
HTMLMediaElement.mozSampleRateRead only- The number of samples per second that will be played. For example, 44100 samples per second is the sample rate used by CD audio.
HTMLMediaElement.srcObject- Lets you set or get the Media Stream to be played or being played.
HTMLMediaElement.muted- Whether audio is muted.
trueif the audio is muted, andfalseotherwise. HTMLMediaElement.networkStateRead only- An
unsigned short(enumeration) indicating the current state of fetching the media over the network. HTMLMediaElement.pausedRead only- Indicates whether the media element is paused.
HTMLMediaElement.playbackRate-
The rate at which the media is being played back.
HTMLMediaElement.playedRead only- The ranges of the media source that the browser has played, if any.
HTMLMediaElement.preload- Reflects the
preloadHTML attribute, indicating what data should be preloaded, if any. Possible values are:none,metadata,auto. Seepreloadattribute documentation for details. HTMLMediaElement.readyStateRead only- An
unsigned short(enumeration) indicating the readiness state of the media. HTMLMediaElement.seekableRead only- The time ranges that the user is able to seek to, if any.
HTMLMediaElement.seekingRead only- Indicates whether the media is in the process of seeking to a new position.
HTMLMediaElement.sinkIdRead only- The unique ID of the audio device delivering output, or an empty string if it is using the user agent default. This ID should be one of the
MediaDeviceInfo.deviceidvalues returned fromMediaDevices.enumeratedDevices(),id-multimedia, orid-communications. HTMLMediaElement.src- Reflects the
srcHTML attribute, containing the URL of a media resource to use. Gecko implements a similar capability for streams:mozSrcObject. HTMLMediaElement.textTracks- Represents the list of
TextTrackobjects contained in the element. HTMLMediaElement.videoTracks- Represents the list of
VideoTrackobjects contained in the element.Note: Gecko supports only single track playback, and the parsing of tracks' metadata is only available for media with the Ogg container format.
HTMLMediaElement.volume- The audio volume, from 0.0 (silent) to 1.0 (loudest).
Event handler properties
HTMLMediaElement.onmozinterruptbegin- Sets the
EventHandlercalled when the media element is interrupted because of the audiochannel manager. HTMLMediaElement.onmozinterruptend- Sets the
EventHandlercalled when the interruption is concluded. - onencrypted
- [description here]
Methods
Inherits methods from its ancestors, HTMLElement, Element, Node, and EventTarget.
HTMLMediaElement.addTextTrack()- Adds a text track (such as a track for subtitles) to a media element.
HTMLMediaElement.canPlayType()- Determines whether the specified media type can be played back.
HTMLMediaElement.fastSeek(double time)- Directly seeks to the given time.
HTMLMediaElement.load()- Resets the media element and restarts the media resource. Any pending events are discarded. How much media data is fetched is still affected by the
preloadattribute. This method can be useful for releasing resources after anysrcattribute andsourceelement descendants have been removed. Otherwise, it is usually unnecessary to use this method, unless required to rescansourceelement children after dynamic changes. HTMLMediaElement.mozGetMetadata()- Returns Object. The
mozGetMetadatamethod returns a javascript object whose properties represent metadata from the playing media resource as {key: value} pairs. A separate copy of the data is returned each time the method is called. This method must be called after theloadedmetadataevent fires. HTMLMediaElement.mozLoadFrom(HTMLMediaElement other)- This method, available only in Mozilla's implementation, loads data from another media element. This works similarly to
load()except that instead of running the normal resource selection algorithm, the source is simply set to theotherelement'scurrentSrc. This is optimized so this element gets access to all of theotherelement's cached and buffered data; in fact, the two elements share downloaded data, so data downloaded by either element is available to both. HTMLMediaElement.pause()- Pauses the media playback.
HTMLMediaElement.play()- Begins playback of the media.
HTMLMediaElement.setMediaKeys- Returns
Promise. Sets theMediaKeyskeys to use when decrypting media during playback. HTMLMediaElement.setSinkId- Returns
Promise. Sets the ID of the audio device through which audio output should be rendered if the application is authorized to play out of a given device.
Specifications
| Specification | Status | Comment |
|---|---|---|
| WHATWG HTML Living Standard The definition of 'HTMLMediaElement' in that specification. |
Living Standard | No change from HTML5 |
| HTML5 The definition of 'HTMLMediaElement' in that specification. |
Recommendation | Initial definition. |
| Encrypted Media Extensions The definition of 'Encrypted Media Extensions' in that specification. |
Working Draft | Adds MediaKeys, MediaEncryptedEvent, and setMediaKeys. |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | 3.5 (1.9.1) [1] | 9 | ? | ? |
buffered property |
(Yes) | 4.0 (2.0) | 9 | ? | ? |
defaultMuted property |
(Yes) | 11.0 (11.0) | ? | ? | ? |
loop property |
(Yes) | 11.0 (11.0) | 9 | ? | ? |
seekable property |
(Yes) | 8.0 (8.0) | 9 | ? | ? |
defaultPlaybackRate property |
(Yes) | 20.0 (20.0) | 9 | ? | ? |
playbackRate property |
(Yes) | 20.0 (20.0) | 9 | ? | ? |
preservesPitch property |
(Yes)webkit | 20.0 (20.0)moz | Not supported | Not supported | Not supported |
preload property |
(Yes) | 4.0 (2.0)[2] | ? | ? | ? |
played property |
(Yes) | 15.0 (15.0) | ? | ? | ? |
crossOrigin property |
(Yes) | 12.0 (12.0)[3] 22.0 (22.0) |
? | ? | ? |
mozAudioChannelType property |
Not supported | Not supported | Not supported | Not supported | Not supported |
mozChannels, mozFrameBufferLength, mozSampleRate properties |
Not supported | 4.0 (2.0) | Not supported | Not supported | Not supported |
mozGetMetadata() |
Not supported | 17.0 (17.0) | Not supported | Not supported | Not supported |
mozLoadFrom() Obsolete since Gecko 24 |
Not supported | 3.6 (1.9.2) | Not supported | Not supported | Not supported |
mozSrcObject property |
Not supported | 18.0 (18.0) | Not supported | Not supported | Not supported |
initialTime property |
? | 9.0 (9.0)[4] | ? | ? | ? |
fastSeek method |
? | 31.0 (31.0) | ? | ? | ? |
startDate property |
? | Not supported[9] | ? | ? | ? |
mediaGroup and controller properties |
? | Not supported[10] | ? | ? | ? |
audioTracks, and videoTracks properties |
? | 33.0 (33.0)[5] | ? | ? | ? |
textTracks property |
? | Nightly build[6] | ? | ? | ? |
sinkId property and setSinkId() method |
45.0[8] | ? | ? | ? | ? |
| Feature | Android | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | ? | (Yes) | (Yes) |
buffered property |
(Yes) | 4.0 (2.0) | (Yes) | ? | ? | ? |
defaultMuted property |
(Yes) | 11.0 (11.0) | (Yes) | ? | ? | ? |
loop property |
(Yes) | 11.0 (11.0) | (Yes) | ? | ? | ? |
seekable property |
(Yes) | 8.0 (8.0) | (Yes) | ? | ? | ? |
defaultPlaybackRate property |
(Yes) | 20.0 (20.0) | (Yes) | ? | ? | ? |
playbackRate property |
(Yes) | 20.0 (20.0) | (Yes) | ? | ? | ? |
preservesPitch property |
(Yes)webkit | 20.0 (20.0)moz | (Yes) | Not supported | Not supported | Not supported |
preload property |
(Yes) | 4.0 (2.0) | (Yes) | ? | ? | ? |
played property |
(Yes) | 15.0 (15.0) | (Yes) | ? | ? | ? |
crossOrigin property |
(Yes) | 12.0 (12.0)[3] 22.0 (22.0) |
(Yes) | ? | ? | ? |
mozAudiochannelType |
Not supported | Not supported | 1.0.1 | Not supported | Not supported | Not supported |
mozChannels, mozFrameBufferLength, mozSampleRate properties |
Not supported | 4.0 (2.0) | (Yes) | Not supported | Not supported | Not supported |
mozGetMetadata() |
Not supported | 17.0 (17.0) | (Yes) | Not supported | Not supported | Not supported |
mozLoadFrom() |
Not supported | 1.0 (1.9.2)[7] | (Yes) | Not supported | Not supported | Not supported |
mozSrcObject property |
Not supported | 18.0 (18.0) | (Yes) | Not supported | Not supported | Not supported |
initialTime property |
? | 9.0 (9.0)[4] | (Yes) | ? | ? | ? |
fastSeek method |
? | 31.0 (31.0) | (Yes) | ? | ? | ? |
startDate property |
? | Not supported[9] | (Yes) | ? | ? | ? |
mediaGroup and controller properties |
? | Not supported[10] | (Yes) | ? | ? | ? |
audioTracks, and videoTracks properties |
? | 33.0 (33.0)[5] | (Yes) | ? | ? | ? |
textTracks property |
? | Nightly build[6] | (Yes) | ? | ? | ? |
sinkId property and setSinkId() method |
? | ? | ? | ? | ? | ? |
[1] The NETWORK_LOADED state was removed to align with the HTML spec in Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1).
[2] Previously Gecko implemented this as autobuffer.
[3] Gecko implements this in lowercase, i.e. crossorigin.
[4] The feature was removed in Gecko 23.0 (Firefox 23 / Thunderbird 23 / SeaMonkey 2.20).
[5] The implementation is hidden behind the media.track.enabled preference.
[6] The implementation is hidden behind the media.webvtt.enabled preference, defaulting there to true.
[7] This feature was removed in 24.0 (24).
[8] This feature is hidden behind a flag.
[9] Gecko doesn't implement this yet. See bug 847376.
[10] Gecko doesn't implement this yet. See bug 847377.
See also
- References
<video>and<audio>HTML elements.HTMLVideoElementandHTMLAudioElementinterfaces, derived fromHTMLMediaElement.
- Articles