이 문서는 아직 자원 봉사자들이 한국어로 번역하지 않았습니다. 함께 해서 번역을 마치도록 도와 주세요!
HTML5 introduces built-in media support via the <audio>
and <video>
elements, offering the ability to easily embed media into HTML documents.
Embedding media
Embedding media in your HTML document is trivial:
The src
attribute can be a URL of the audio file or the path to the file on the local system.
This code example uses attributes of the <audio>
element:
controls
: Displays the standard HTML5 controls for the audio on the web page.autoplay
: Makes the audio play automatically.loop
: Make the audio repeat (loop) automatically.
The preload
attribute is used in the audio element for buffering large files. It can take one of 3 values:
"none"
does not buffer the file"auto"
buffers the media file"metadata"
buffers only the metadata for the file
Multiple source files can be specified using the <source>
element in order to provide video or audio encoded in different formats for different browsers. For instance:
<video controls> <source src="SampleVideo.ogv" type="video/ogv"> <source src="SampleVideo.mp4" type="video/mp4"> Your browser does not support the <code>video</code> element. </video>
This plays the Ogg video file in browsers supporting the Ogg format. If the browser doesn't support Ogg video, the browser uses the MPEG-4 file. See also the list of media formats supported by the audio and video elements in different browsers.
You may also specify which codecs the media file requires; this allows the browser to make even more intelligent decisions:
<video controls> <source src="SampleVideo.ogv" type="video/ogv; codecs=dirac, speex"> Your browser does not support the <code>video</code> element. </video>
Here, we specify that the video uses the Dirac and Speex codecs. If the browser supports Ogg video, but not the specified codecs, the video will not load.
If the type
attribute isn't specified, the media's type is retrieved from the server and checked to see if the browser can handle it; if it can't be rendered, the next source
is checked. If none of the specified source
elements can be used, an error
event is dispatched to the video
element. If the type
attribute is specified, it's compared against the types the browser can play, and if it's not recognized, the server doesn't even get queried; instead, the next source
is checked at once.
See Media events for a complete list of events associated with media playback. For details on media formats supported by different browsers, see Media formats supported by the audio and video elements.
Controlling media playback
Once you've embedded media into your HTML document using the new elements, you can programmatically control them from JavaScript code. For example, to start (or restart) playback, you can do this:
var v = document.getElementsByTagName("video")[0]; v.play();
The first line fetches the first video element in the document, and the second calls the element's play()
method, as defined in the nsIDOMHTMLMediaElement
interface that is used to implement the media elements.
Controlling an HTML5 audio player to play, pause, increase and decrease volume using some Javascript is straightforward.
<audio id="demo" src="audio.mp3"></audio> <div> <button onclick="document.getElementById('demo').play()">Play the Audio</button> <button onclick="document.getElementById('demo').pause()">Pause the Audio</button> <button onclick="document.getElementById('demo').volume+=0.1">Increase Volume</button> <button onclick="document.getElementById('demo').volume-=0.1">Decrease Volume</button> </div>
Stopping the download of media
While stopping the playback of media is as easy as calling the element's pause() method, the browser keeps downloading the media until the media element is disposed of through garbage collection.
Here's a trick that stops the download at once:
var mediaElement = document.getElementById("myMediaElementID"); mediaElement.removeAttribute("src");
mediaElement.load();
By removing the media element's src
attribute and invoking the load() method, you release the resources associated with the video, which stops the network download. You must call load() after removing the attribute because just removing the src attribute does not invoke the load algorithm. If the video element also has source element descendants, those should also be removed before calling load().
Note that just setting the src attribute to an empty string will actually cause the browser to treat it as though you're setting a video source to a relative path, causing the browser to attempt another download to something that is unlikely to be a valid video.
Seeking through media
Media elements provide support for moving the current playback position to specific points in the media's content. This is done by setting the value of the currentTime
property on the element; see HTMLMediaElement
for further details on the element's properties. Simply set the value to the time, in seconds, at which you want playback to continue.
You can use the element's seekable
property to determine the ranges of the media that are currently available for seeking to. This returns a TimeRanges
object listing the ranges of times that you can seek to.
var mediaElement = document.getElementById('mediaElementID'); mediaElement.seekable.start(0); // Returns the starting time (in seconds) mediaElement.seekable.end(0); // Returns the ending time (in seconds) mediaElement.currentTime = 122; // Seek to 122 seconds mediaElement.played.end(0); // Returns the number of seconds the browser has played
Specifying playback range
When specifying the URI of media for an <audio>
or <video>
element, you can optionally include additional information to specify the portion of the media to play. To do this, append a hash mark ("#") followed by the media fragment description.
A time range is specified using the syntax:
#t=[starttime][,endtime]
The time can be specified as a number of seconds (as a floating-point value) or as an hours/minutes/seconds time separated with colons (such as 2:05:01 for 2 hours, 5 minutes, and 1 second).
A few examples:
- https://example.com/video.ogv#t=10,20
- Specifies that the video should play the range 10 seconds through 20 seconds.
- https://example.com/video.ogv#t=,10.5
- Specifies that the video should play from the beginning through 10.5 seconds.
- https://example.com/video.ogv#t=,02:00:00
- Specifies that the video should play from the beginning through two hours.
- https://example.com/video.ogv#t=60
- Specifies that the video should start playing at 60 seconds and play through the end of the video.
The playback range portion of the media element URI specification was added to Gecko 9.0 (Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6). At this time, this is the only part of the Media Fragments URI specification implemented by Gecko, and it can only be used when specifying the source for media elements, and not in the address bar.
Fallback options
HTML included between, for example, the opening and closing tags of media elements is processed by browsers that don't support HTML5 media. You can take advantage of this fact to provide alternative fallback media for those browsers.
This section provides two possible fallback options for video. In each case, if the browser supports HTML5 video, that is used; otherwise, the fallback option is used.
Using Flash
You can use Flash to play a Flash format movie if the <video>
element isn't supported:
<video src="video.ogv" controls> <object data="flvplayer.swf" type="application/x-shockwave-flash"> <param value="flvplayer.swf" name="movie"/> </object> </video>
Note that you shouldn't include classid
in the object
tag in order to be compatible with browsers other than Internet Explorer.
Playing Ogg videos using a Java applet
There's a Java applet called Cortado that you can use as a fallback to play Ogg videos in browsers that have Java support but don't support HTML5 video:
<video src="my_ogg_video.ogv" controls width="320" height="240"> <object type="application/x-java-applet" width="320" height="240"> <param name="archive" value="cortado.jar"> <param name="code" value="com.fluendo.player.Cortado.class"> <param name="url" value="my_ogg_video.ogv"> <p>You need to install Java to play this file.</p> </object> </video>
If you do not create an alternate child element of the cortado object element, such as the <p>
element above, FireFox 3.5 installations that handle the video natively but do not have Java installed will incorrectly inform the user that they need to install a plugin to view content on the page.
Note that Cortado is no longer maintained, and the Java plugin no longer works in many browsers. For the related need of serving Ogg or WebM files to modern browsers with MP4-only media implementations such as Safari, IE 11, or Edge, consider the JavaScript-based ogv.js if you cannot provide your media in MP4 formats as well.
Error handling
Requires Gecko 2.0(Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)Starting in Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1), error handling has been revised to match the latest version of the HTML5 specification. Instead of the error
event being dispatched to the media element itself, it now gets delivered to the child <source>
elements corresponding to the sources resulting in the error.
This lets you detect which sources failed to load, which may be useful. Consider this HTML:
<video> <source id="mp4_src" src="video.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'> </source> <source id="3gp_src" src="video.3gp" type='video/3gpp; codecs="mp4v.20.8, samr"'> </source> <source id="ogg_src" src="video.ogv" type='video/ogv; codecs="theora, vorbis"'> </source> </video>
Since Firefox doesn't support MP4 and 3GP on some platforms due to their patent-encumbered nature, the <source>
elements with the IDs "mp4_src" and "3gp_src" will receive error
events before the Ogg resource is loaded. The sources are tried in the order in which they appear, and once one loads successfully, the remaining sources aren't tried at all.
Detecting when no sources have loaded
To detect that all child <source>
elements have failed to load, check the value of the media element's networkState
attribute. If this is HTMLMediaElement.NETWORK_NO_SOURCE
, you know that all the sources failed to load.
If at that point you add another source by inserting a new <source>
element as a child of the media element, Gecko attempts to load the specified resource.
Showing fallback content when no source could be decoded
Another way to show the fallback content of a video when none of the sources could be decoded in the current browser is to add an error handler on the last source element. Then you can replace the video with its fallback content:
<video controls> <source src="dynamicsearch.mp4" type="video/mp4"></source> <a href="dynamicsearch.mp4"> <img src="dynamicsearch.jpg" alt="Dynamic app search in Firefox OS"> </a> <p>Click image to play a video demo of dynamic app search</p> </video>
var v = document.querySelector('video'), sources = v.querySelectorAll('source'), lastsource = sources[sources.length-1]; lastsource.addEventListener('error', function(ev) { var d = document.createElement('div'); d.innerHTML = v.innerHTML; v.parentNode.replaceChild(d, v); }, false);
See also
- The media-related HTML elements:
<audio>
,<video>
,<source>
; - Manipulating video using canvas
- Introducing the Audio API Extension
nsIDOMHTMLMediaElement
- Media formats supported by the audio and video elements
- Theora Cookbook
- Popcorn.js - The HTML5 Media Framework
- Kaltura Video Library Solution, a JavaScript library (mwEmbed) which supports a seamless fallback with HTML5, VLC Player, Java Cortado and OMTK Flash Vorbis player. (It is used by Wikimedia)
- OMTK - Flash, a Flash library which implements a Vorbis decoder
- Projekktor Player, a JavaScript wrapper for audio- and video-tags with flash fallback, open source, GPL
- Applet Cortado, an audio/video playback solution in Java by Xiph.org (no longer maintained)
- Video.JS, an open source HTML5 video player and framework.
- MediaElement.js - open source HTML5 audio/video framework with a custom Flash shim that mimic HTML5 media API for older browsers.
- Flv Player - HTML5 fallback support video player
- DASH - Dynamic Adaptive Streaming over HTTP - for HTML5 video
- jPlayer an open source audio and video libray for jQuery and Zepto
- ogv.js a JavaScript player for Ogg audio and video mimicking HTML5 media API, replacing the obsolete Cortado Java applet for cross-browser Ogg/WebM playback