dom / latest / htmlmediaelement.html /

HTMLMediaElement

The HTMLMediaElement interface adds to HTMLElement the properties and methods needed to support basic media-related capabilities that are common to audio and video.

The HTMLVideoElement and HTMLAudioElement elements both inherit this interface.

EventTarget Node Element HTMLElement HTMLMediaElement

Properties

This interface also inherits properties from its ancestors HTMLElement, Element, Node, and EventTarget.

HTMLMediaElement.audioTracks

A AudioTrackList that lists the AudioTrack objects contained in the element.

HTMLMediaElement.autoplay

A boolean value that reflects the autoplay HTML attribute, indicating whether playback should automatically begin as soon as enough media is available to do so without interruption.

Note: Automatically playing audio when the user doesn't expect or desire it is a poor user experience and should be avoided in most cases, though there are exceptions. See the Autoplay guide for media and Web Audio APIs for more information. Keep in mind that browsers may ignore autoplay requests, so you should ensure that your code isn't dependent on autoplay working.

HTMLMediaElement.buffered Read only

Returns a TimeRanges object that indicates the ranges of the media source that the browser has buffered (if any) at the moment the buffered property is accessed.

HTMLMediaElement.controls

Is a Boolean that reflects the controls HTML attribute, indicating whether user interface items for controlling the resource should be displayed.

HTMLMediaElement.controlsList Read only

Returns a DOMTokenList that helps the user agent select what controls to show on the media element whenever the user agent shows its own set of controls. The DOMTokenList takes one or more of three possible values: nodownload, nofullscreen, and noremoteplayback.

HTMLMediaElement.crossOrigin

A DOMString indicating the CORS setting for this media element.

HTMLMediaElement.currentSrc Read only

Returns a DOMString with the absolute URL of the chosen media resource.

HTMLMediaElement.currentTime

A double-precision floating-point value indicating the current playback time in seconds; if the media has not started to play and has not been seeked, this value is the media's initial playback time. Setting this value seeks the media to the new time. The time is specified relative to the media's timeline.

HTMLMediaElement.defaultMuted

A Boolean that reflects the muted HTML attribute, which indicates whether the media element's audio output should be muted by default.

HTMLMediaElement.defaultPlaybackRate

A double indicating the default playback rate for the media.

HTMLMediaElement.disableRemotePlayback

A Boolean that sets or returns the remote playback state, indicating whether the media element is allowed to have a remote playback UI.

HTMLMediaElement.duration Read only

A read-only double-precision floating-point value indicating the total duration of the media in seconds. If no media data is available, the returned value is NaN. If the media is of indefinite length (such as streamed live media, a WebRTC call's media, or similar), the value is +Infinity.

HTMLMediaElement.ended Read only

Returns a Boolean that indicates whether the media element has finished playing.

HTMLMediaElement.error Read only

Returns a MediaError object for the most recent error, or null if there has not been an error.

HTMLMediaElement.loop

A Boolean that reflects the loop HTML attribute, which indicates whether the media element should start over when it reaches the end.

HTMLMediaElement.mediaKeys Read only Experimental

Returns a MediaKeys object or null. MediaKeys is a set of keys that an associated HTMLMediaElement can use for decryption of media data during playback.

HTMLMediaElement.muted

Is a Boolean that determines whether audio is muted. true if the audio is muted and false otherwise.

HTMLMediaElement.networkState Read only

Returns a unsigned short (enumeration) indicating the current state of fetching the media over the network.

HTMLMediaElement.paused Read only

Returns a Boolean that indicates whether the media element is paused.

HTMLMediaElement.playbackRate

Is a double that indicates the rate at which the media is being played back.

HTMLMediaElement.played Read only

Returns a TimeRanges object that contains the ranges of the media source that the browser has played, if any.

HTMLMediaElement.preload

Is a DOMString that reflects the preload HTML attribute, indicating what data should be preloaded, if any. Possible values are: none, metadata, auto.

HTMLMediaElement.preservesPitch

Is a boolean value that determines if the pitch of the sound will be preserved. If set to false, the pitch will adjust to the speed of the audio.

HTMLMediaElement.readyState Read only

Returns a unsigned short (enumeration) indicating the readiness state of the media.

HTMLMediaElement.seekable Read only

Returns a TimeRanges object that contains the time ranges that the user is able to seek to, if any.

HTMLMediaElement.seeking Read only

Returns a Boolean that indicates whether the media is in the process of seeking to a new position.

HTMLMediaElement.sinkId Read only Experimental

Returns a DOMString that is 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.deviceid values returned from MediaDevices.enumerateDevices(), id-multimedia, or id-communications.

HTMLMediaElement.src

Is a DOMString that reflects the src HTML attribute, which contains the URL of a media resource to use.

HTMLMediaElement.srcObject

Is a MediaStream representing the media to play or that has played in the current HTMLMediaElement, or null if not assigned.

HTMLMediaElement.textTracks Read only

Returns a TextTrackList object containing the list of TextTrack objects contained in the element.

HTMLMediaElement.videoTracks Read only

Returns a VideoTrackList object containing the list of VideoTrack objects contained in the element.

HTMLMediaElement.volume

Is a double indicating the audio volume, from 0.0 (silent) to 1.0 (loudest).

Event handlers

HTMLMediaElement.onencrypted

Sets the event handler called when the media is encrypted.

HTMLMediaElement.onwaitingforkey

Sets the event handler called when playback is blocked while waiting for an encryption key.

Obsolete properties

These properties are obsolete and should not be used, even if a browser still supports them.

HTMLMediaElement.controller Deprecated

Is a MediaController object that represents the media controller assigned to the element, or null if none is assigned.

HTMLMediaElement.mediaGroup Deprecated

A DOMString that reflects the mediagroup HTML attribute, which indicates the name of the group of elements it belongs to. A group of media elements shares a common MediaController.

HTMLMediaElement.mozAudioCaptured Read only Non-Standard Deprecated

Returns a Boolean. Related to audio stream capture.

HTMLMediaElement.mozFragmentEnd Non-Standard Deprecated

Is a double that provides access to the fragment end time if the media element has a fragment URI for currentSrc, otherwise it is equal to the media duration.

Methods

This interface also inherits methods from its ancestors HTMLElement, Element, Node, and EventTarget.

HTMLMediaElement.addTextTrack()

Adds a new TextTrack object (such as a track for subtitles) to a media element. This is a programmatic interface only and does not affect the DOM.

HTMLMediaElement.captureStream() Experimental

Returns MediaStream, captures a stream of the media content.

HTMLMediaElement.canPlayType()

Given a string specifying a MIME media type (potentially with the codecs parameter included), canPlayType() returns the string probably if the media should be playable, maybe if there's not enough information to determine whether the media will play or not, or an empty string if the media cannot be played.

HTMLMediaElement.fastSeek() Experimental

Quickly seeks to the given time with low precision.

HTMLMediaElement.load()

Resets the media to the beginning and selects the best available source from the sources provided using the src attribute or the <source> element.

HTMLMediaElement.pause()

Pauses the media playback.

HTMLMediaElement.play()

Begins playback of the media.

HTMLMediaElement.seekToNextFrame() Non-Standard Experimental Deprecated

Seeks to the next frame in the media. This non-standard, experimental method makes it possible to manually drive reading and rendering of media at a custom speed, or to move through the media frame-by-frame to perform filtering or other operations.

HTMLMediaElement.setMediaKeys() Experimental

Returns Promise. Sets the MediaKeys keys to use when decrypting media during playback.

HTMLMediaElement.setSinkId() Experimental

Sets the ID of the audio device to use for output and returns a Promise. This only works when the application is authorized to use the specified device.

Obsolete methods

These methods are obsolete and should not be used, even if a browser still supports them.

HTMLMediaElement.mozCaptureStream() Non-Standard

[enter description]

HTMLMediaElement.mozCaptureStreamUntilEnded() Non-Standard Deprecated

[enter description]

HTMLMediaElement.mozGetMetadata() Non-Standard Deprecated

Returns Object, which contains properties that 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 the loadedmetadata event fires.

Events

Inherits methods from its parent, HTMLElement , defined in the GlobalEventHandlers mixin. Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface.

abort

Fired when the resource was not fully loaded, but not as the result of an error.

canplay

Fired when the user agent can play the media, but estimates that not enough data has been loaded to play the media up to its end without having to stop for further buffering of content

canplaythrough

Fired when the user agent can play the media, and estimates that enough data has been loaded to play the media up to its end without having to stop for further buffering of content.

durationchange

Fired when the duration property has been updated.

emptied

Fired when the media has become empty; for example, when the media has already been loaded (or partially loaded), and the HTMLMediaElement.load() method is called to reload it.

ended

Fired when playback stops when end of the media (<audio> or <video>) is reached or because no further data is available.

error

Fired when the resource could not be loaded due to an error.

loadeddata

Fired when the first frame of the media has finished loading.

loadedmetadata

Fired when the metadata has been loaded

loadstart

Fired when the browser has started to load a resource.

pause

Fired when a request to pause play is handled and the activity has entered its paused state, most commonly occurring when the media's HTMLMediaElement.pause() method is called.

play

Fired when the paused property is changed from true to false, as a result of the HTMLMediaElement.play() method, or the autoplay attribute

playing

Fired when playback is ready to start after having been paused or delayed due to lack of data

progress

Fired periodically as the browser loads a resource.

ratechange

Fired when the playback rate has changed.

resize

Fired when one or both of the videoWidth and videoHeight properties have just been updated.

seeked

Fired when a seek operation completes

seeking

Fired when a seek operation begins

stalled

Fired when the user agent is trying to fetch media data, but data is unexpectedly not forthcoming.

suspend

Fired when the media data loading has been suspended.

timeupdate

Fired when the time indicated by the currentTime property has been updated.

volumechange

Fired when the volume has changed.

waiting

Fired when playback has stopped because of a temporary lack of data.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
HTMLMediaElement
1
12
3.5
5.5
10.5
3.1
1
18
4
11
3
1.0
abort_event
1
12
9
9
≤12.1
3.1
1
18
9
≤12.1
3
1.0
addTextTrack
26
23
12
31
11
≤12.1
6
≤37
4.4
26
25
31
≤12.1
6
1.5
1.5
audioTracks
37
79
12-79
33
10
24
8
No
37
No
24
8
No
autoplay
1
12
3.5
9
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
buffered
1
12
4
9
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
canplay_event
3
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
canplaythrough_event
3
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
canPlayType
3
12
3.5
Before Firefox 28, canPlayType() returned probably when asked about WebM audio or video files without the codecs parameter. Since multiple codecs are supported, this is not enough information to determine if a file can be played, so maybe is now correctly returned.
9
≤12.1
4
≤37
18
4
≤12.1
3
1.0
captureStream
62
79
15
No
49
No
62
62
15
46
No
8.0
controller
No
No
No
Firefox doesn't implement this yet. See bug 847377.
No
No
6
No
No
No
Firefox doesn't implement this yet. See bug 847377.
No
6
No
controls
1
12
3.5
9
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
controlsList
58
79
No
No
45
No
58
58
No
43
No
7.0
crossOrigin
33
13
22
12-22
No
20
10
4.4.3
33
22
14-22
20
10
2.0
currentSrc
1
12
3.5
9
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
currentTime
1
12
3.5
9
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
defaultMuted
15
12
11
No
≤12.1
6
4.4
18
14
≤12.1
6
1.0
defaultPlaybackRate
1
12
20
9
≤12.1
3.1
≤37
18
20
≤12.1
3
1.0
disableRemotePlayback
49
79
No
No
36
13.1
No
See bug 521319.
49
No
36
13
5.0
duration
1
12
3.5
9
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
durationchange_event
3
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
emptied_event
3
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
encrypted_event
42
13
38
No
29
12.1
42
42
38
29
12.2
4.0
ended
1
12
3.5
9
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
ended_event
3
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
error
1
12
3.5
9
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
error_event
1
12
6
9
11.6
3.1
1
18
6
12
3
1.0
fastSeek
No
See bug 648207.
No
See bug 648207.
31
No
No
See bug 648207.
8
No
See bug 648207.
No
See bug 648207.
31
No
See bug 648207.
8
No
See bug 648207.
getStartDate
No
No
No
No
No
9
No
No
No
No
9
No
load
1
12
3.5
9
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
loadeddata_event
3
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
loadedmetadata_event
3
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
loadstart_event
1
12
6
9
≤12.1
4
1
18
6
≤12.1
3
1.0
loop
3
12
11
9
≤12.1
4
≤37
18
14
≤12.1
3
1.0
mediaGroup
No
No
No
Firefox doesn't implement this yet. See bug 847377.
No
No
6
No
No
No
Firefox doesn't implement this yet. See bug 847377.
No
6
No
mediaKeys
42
13
38
No
29
12.1
42
42
38
29
12.2
4.0
mozAudioCaptured
No
No
15
No
No
No
No
No
15
No
No
No
mozCaptureStreamUntilEnded
No
No
15
No
No
No
No
No
15
No
No
No
mozFragmentEnd
No
No
9
No
No
No
No
No
9
No
No
No
mozGetMetadata
No
No
17
No
No
No
No
No
17
No
No
No
muted
1
12
3.5
9
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
networkState
1
12
3.5
The NETWORK_LOADED state was removed to align with the HTML spec in Firefox 4.
9
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
pause
1
12
3.5
9
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
pause_event
3
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
paused
1
12
3.5
9
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
play
1
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
play_event
3
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
playbackRate
1
12
20
9
≤12.1
3.1
≤37
18
20
≤12.1
3
1.0
played
1
12
15
9
≤12.1
3.1
≤37
18
15
≤12.1
3
1.0
playing_event
3
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
preload
5
12
4
9
≤12.1
5
3
18
4
≤12.1
5
1.0
preservesPitch
86
86
20
No
72
4
See bug 214922.
86
86
20
61
4
See bug 214922.
14.0
progress_event
1
12
6
9
≤12.1
3.1
1
18
6
≤12.1
3
1.0
ratechange_event
3
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
readyState
1
12
3.5
5.5
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
remote
56
79
No
No
43
13.1
No
See bug 521319.
56
No
43
13.4
6.0
seekable
1
12
8
9
≤12.1
3.1
≤37
18
8
≤12.1
3
1.0
seeked_event
3
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
seeking
1
12
3.5
9
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
seeking_event
3
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
seekToNextFrame
No
No
56
No
No
No
No
No
56
No
No
No
setMediaKeys
42
13
38
No
29
12.1
42
42
38
29
12.2
4.0
setSinkId
49
17
64
No
36
No
No
Not availabe due to a limitation in Android.
No
Not availabe due to a limitation in Android.
No
36
No
No
Not availabe due to a limitation in Android.
sinkId
49
17
No
No
36
No
No
49
No
36
No
5.0
src
1
12
3.5
9
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
srcObject
52
Only supports MediaStream objects (see bug 506273).
12
Only supports MediaStream objects (see bug 506273).
42
Only supports MediaStream objects (see bug 886194).
18-58
No
39
Only supports MediaStream objects (see bug 506273).
11
52
Only supports MediaStream objects (see bug 506273.
52
Only supports MediaStream objects (see bug 506273).
42
Only supports MediaStream objects (see bug 886194).
18-58
41
Only supports MediaStream objects (see bug 506273).
11
6.0
Only supports MediaStream objects (see bug 506273).
stalled_event
3
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
suspend_event
3
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
textTracks
23
12
31
10
≤12.1
6
4.4
25
31
≤12.1
6
1.0
timeupdate_event
3
12
3.5
9
10.5
3.1
≤37
18
4
11
3
1.0
videoTracks
37
79
12-79
33
No
24
8
No
37
No
24
8
No
volume
1
12
3.5
9
≤12.1
3.1
≤37
18
4
≤12.1
3
1.0
volumechange_event
1
12
6
9
≤12.1
3.1
≤37
18
6
≤12.1
3
1.0
waiting_event
1
12
6
9
≤12.1
3.1
≤37
18
6
≤12.1
3
1.0
waitingforkey_event
55
79
52
No
42
12.1
55
55
52
42
12.2
6.0

See also

References

Guides

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement