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 (en-US) and HTMLAudioElement (en-US) elements both inherit this interface.

屬性

這個介面從HTMLElementElementNode,和EventTarget繼承了屬性

HTMLMediaElement.audioTracks (en-US)
AudioTrackList (en-US) 會列出包含在該媒體元素內部的AudioTrack (en-US)物件。
HTMLMediaElement.autoplay (en-US)
是一個布林值,表達了HTML中是否有autoplay (en-US)屬性;意即;表明是否只要在媒體可以播放且不中斷的時候,能夠自動播放。
在網站上自動播放音訊(或是有音訊的影片),可能是惱人的使用者體驗;因此,應該盡可能地避免。如果你必須要有自動播放的功能,你應該讓它是可選擇的(讓使用者特地去啟動)。 However, this can be useful when creating media elements whose source will be set at a later time, under user control.
HTMLMediaElement.buffered (en-US) Read only
回傳一個TimeRanges (en-US)物件,表示媒體當下buffered屬性被瀏覽器存取時的緩存(如果有的話)區間。
HTMLMediaElement.controller (en-US)
Is a MediaController object that represents the media controller assigned to the element, or null if none is assigned.
HTMLMediaElement.controls (en-US)
Is a Boolean that reflects the controls (en-US) HTML attribute, indicating whether user interface items for controlling the resource should be displayed.
HTMLMediaElement.controlsList (en-US) 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 (en-US)
Is a DOMString indicating the CORS setting for this media element.
HTMLMediaElement.currentSrc (en-US) Read only
Returns a DOMString with the absolute URL of the chosen media resource.
HTMLMediaElement.currentTime (en-US)
Is a double indicating the current playback time in seconds. Setting this value seeks the media to the new time.
HTMLMediaElement.defaultMuted (en-US)
Is a Boolean that reflects the muted (en-US) HTML attribute, which indicates whether the media element's audio output should be muted by default.
HTMLMediaElement.defaultPlaybackRate (en-US)
Is a double indicating the default playback rate for the media.
HTMLMediaElement.disableRemotePlayback (en-US)
Is 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 (en-US) Read only
Returns a double indicating the length of the media in seconds, or 0 if no media data is available.
HTMLMediaElement.ended (en-US) Read only
Returns a Boolean that indicates whether the media element has finished playing.
HTMLMediaElement.error (en-US) Read only
Returns a MediaError (en-US) object for the most recent error, or null if there has not been an error.
HTMLMediaElement.loop (en-US)
Is a Boolean that reflects the loop (en-US) HTML attribute, which indicates whether the media element should start over when it reaches the end.
HTMLMediaElement.mediaGroup (en-US)
Is a DOMString that reflects the mediagroup (en-US) HTML attribute, which indicates the name of the group of elements it belongs to. A group of media elements shares a common MediaController.
HTMLMediaElement.mediaKeys Read only
Returns a MediaKeys (en-US) object or null. MediaKeys is a set of keys that an associated HTMLMediaElement can use for decryption of media data during playback.
HTMLMediaElement.mozAudioCaptured Read only
Returns a Boolean. Related to audio stream capture.
HTMLMediaElement.mozFragmentEnd
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.
HTMLMediaElement.mozFrameBufferLength  

Is a unsigned long that indicates the number of samples that will be returned in the framebuffer of each MozAudioAvailable event. 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 mozFrameBufferLength property can be set to a new value for lower latency, 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 the loadedmetadata event fires, when the audio info is known, but before the audio has started or MozAudioAvailable events have begun firing.

HTMLMediaElement.mozSampleRate Read only  
Returns a double representing 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.muted (en-US)
Is a Boolean that determines whether audio is muted. true if the audio is muted and false otherwise.
HTMLMediaElement.networkState (en-US) Read only
Returns a unsigned short (enumeration) indicating the current state of fetching the media over the network.
HTMLMediaElement.paused (en-US) Read only
Returns a Boolean that indicates whether the media element is paused.
HTMLMediaElement.playbackRate (en-US)
Is a double that indicates the rate at which the media is being played back. 
HTMLMediaElement.played Read only
Returns a TimeRanges (en-US) 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 (en-US) HTML attribute, indicating what data should be preloaded, if any. Possible values are: none, metadata, auto.
HTMLMediaElement.preservesPitch
Is a Boolean 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. This is implemented with prefixes in Firefox (mozPreservesPitch) and WebKit (webkitPreservesPitch).
HTMLMediaElement.readyState Read only
Returns a unsigned short (enumeration) indicating the readiness state of the media.
HTMLMediaElement.seekable (en-US) Read only
Returns a TimeRanges (en-US) 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 (en-US) Read only
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() (en-US), id-multimedia, or id-communications.
HTMLMediaElement.src (en-US)
Is a DOMString that reflects the src (en-US) HTML attribute, which contains the URL of a media resource to use.
HTMLMediaElement.srcObject (en-US)
Is a MediaStream (en-US) representing the media to play or that has played in the current HTMLMediaElement, or null if not assigned.
HTMLMediaElement.textTracks (en-US) Read only
Returns the list of TextTrack (en-US) objects contained in the element.
HTMLMediaElement.videoTracks (en-US) Read only
Returns the list of VideoTrack (en-US) objects contained in the element.

Gecko supports only single track playback, and the parsing of tracks' metadata is only available for media with the Ogg container format.

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

Event handlers

 
HTMLMediaElement.onencrypted (en-US)
Sets the EventHandler (en-US) called when the media is encrypted.
HTMLMediaElement.onwaitingforkey (en-US)
Sets the EventHandler (en-US) called when playback is blocked while waiting for an encryption key.

Obsolete attributes

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

 

HTMLMediaElement.initialTime Read only This is an obsolete API and is no longer guaranteed to work.
Returns a double that indicates the initial playback position in seconds.
HTMLMediaElement.mozChannels Read only  
Returns a double representing the number of channels in the audio resource (e.g., 2 for stereo).

Obsolete event handlers

 

HTMLMediaElement.onmozinterruptbegin This is an obsolete API and is no longer guaranteed to work.
Sets the event handler called when the media element is interrupted because of the Audio Channel manager. This was Firefox-specific, having been implemented for Firefox OS, and was removed in Firefox 55.
HTMLMediaElement.onmozinterruptend This is an obsolete API and is no longer guaranteed to work.
Sets the EventHandler (en-US) called when the interruption is concluded. This was Firefox-specific, having been implemented for Firefox OS, and was removed in Firefox 55.

 

 

Methods

This interface also 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.captureStream() (en-US)
Returns MediaStream (en-US), captures a stream of the media content.
HTMLMediaElement.canPlayType() (en-US)
Determines whether the specified media type can be played back.
HTMLMediaElement.fastSeek() (en-US)
Directly seeks to the given time.
HTMLMediaElement.load() (en-US)
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 preload attribute. This method can be useful for releasing resources after any src attribute and source element descendants have been removed. Otherwise, it is usually unnecessary to use this method, unless required to rescan source element children after dynamic changes.
HTMLMediaElement.mozCaptureStream()
[enter description]
HTMLMediaElement.mozCaptureStreamUntilEnded()
[enter description]
HTMLMediaElement.mozGetMetadata()
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.
HTMLMediaElement.pause() (en-US)
Pauses the media playback.
HTMLMediaElement.play() (en-US)
Begins playback of the media.
HTMLMediaElement.seekToNextFrame() (en-US)
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() (en-US)
Returns Promise. Sets the MediaKeys (en-US) keys to use when decrypting media during playback.
HTMLMediaElement.setSinkId() (en-US)
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.mozLoadFrom()
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 the other element's currentSrc. This is optimized so this element gets access to all of the other element's cached and buffered data; in fact, the two elements share downloaded data, so data downloaded by either element is available to both.

 

Specifications

Specification Status Comment
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.
Recommendation Adds MediaKeys (en-US), MediaEncryptedEvent, setMediaKeys, onencrypted, and onwaitingforkey.
Media Capture and Streams
The definition of 'HTMLMediaElement' in that specification.
Candidate Recommendation Adds sinkId and setSinkId(), and captureStream().

Browser compatibility

BCD tables only load in the browser

See also