Java Reference
In-Depth Information
Or you can create one dynamically using
document.createElement()
, like this:
var video = document.createElement("video");
And once you have an
HTMLMediaElement
object, you can begin to program it with its robust API.
methods
Media objects have just a handful of methods, and they're primarily used for controlling media
playback, as shown in the following table.
method name
desCription
Determines the likelihood that the browser can play media of the
provided MIME type and/or codec
canPlayType(mimeType)
Begins to load the media from the server
load()
Pauses the media playback
pause()
Begins or continues the playback of the media
play()
These are the methods defined by the HTML5 specification, but be aware that the various browsers
can also implement their own methods in addition to these four. For example, Firefox adds many
more methods to
HTMLMediaElement
objects. This topic, however, does not cover them.
The
pause()
and
play()
methods are straightforward; you use them to pause and play the media,
respectively:
video.play();
video.pause();
The other two methods are used when you want to load media dynamically. The
load()
method,
obviously, tells the browser to load the specified media. The
canPlayType()
method, however, is
a bit more involved because it doesn't return
true
or
false
. Instead, it returns a variety of values
indicating the
likelihood
that the browser supports the given type. The possible values returned by
canPlayType()
are:
➤
"probably"
: Indicates that the type appears to be playable
➤
"maybe"
: It's impossible to tell if the type is playable without actually playing it.
➤
""
: The media definitely cannot be played.
The
canPlayType()
and
load()
methods are only needed if you plan to load a video dynamically.
Here's an example of how that code could look:
if (video.canPlayType("video/webm") == "probably") {
video.src = "bbb.webm";
} else {
video.src = "bbb.mp4";