{"id":4232,"date":"2010-03-01T19:02:37","date_gmt":"2010-03-01T18:02:37","guid":{"rendered":"http:\/\/www.chipwreck.de\/blog\/?p=4232"},"modified":"2016-12-31T02:19:20","modified_gmt":"2016-12-31T01:19:20","slug":"html-5-video-dom-attributes-and-events","status":"publish","type":"post","link":"https:\/\/www.chipwreck.de\/blog\/2010\/03\/01\/html-5-video-dom-attributes-and-events\/","title":{"rendered":"HTML 5 Video \u2013 DOM Attributes and Events"},"content":{"rendered":"<p>The last <a href=\"\/blog\/2010\/02\/25\/html-5-video-tag-and-attributes\/\">last post<\/a> was about the HTML 5 video tag, now we&#8217;ll have a look at the DOM side of things: Attributes and Events.<!--more--><\/p>\n<h4>Attributes<\/h4>\n<p>Additional to the standard attributes (like width, height, id&#8230;) there are some attributes specific to the video-tag.<\/p>\n<p class=\"small\">To be precise: These attributes are not all specific to the video tag, some are also used for the audio-tag.<\/p>\n<h5>Display attributes<\/h5>\n<p><kbd>src<\/kbd> <small>(string)<\/small>: The source file<\/p>\n<p><kbd>poster<\/kbd> <small>(URL)<\/small>: An image to display before the video is playing<\/p>\n<p><kbd>controls<\/kbd> <small>(boolean)<\/small>: Are the playback-controls being provided by the browser?<\/p>\n<p><kbd>videoWidth, videoHeight<\/kbd> <small>(integer)<\/small>: The original video size<\/p>\n<h5>Playback attributes<\/h5>\n<p><kbd>currentTime<\/kbd> <small>(float)<\/small>: The current playback time in seconds<\/p>\n<p><kbd>startTime*<\/kbd> <small>(float)<\/small>: The video start time (if the video does not start at 0.0 &#8211; streams for example)<\/p>\n<p><kbd>duration<\/kbd> <small>(float)<\/small>: The duration in seconds<\/p>\n<p><kbd>paused<\/kbd> <small>(boolean)<\/small>: Is the video currently paused?<\/p>\n<p><kbd>ended<\/kbd> <small>(boolean)<\/small>: Has the video ended?<\/p>\n<p><kbd>autoplay<\/kbd> <small>(boolean)<\/small>: Is is set to autoplay?<\/p>\n<p><kbd>loop<\/kbd> <small>(boolean)<\/small>: Is is set to play in a loop?<\/p>\n<p><kbd>autobuffer<\/kbd> <small>(boolean)<\/small>: Should the browser start buffering the video immediately?<\/p>\n<p><kbd>seeking<\/kbd> <small>(boolean)<\/small>: Is the browser currently seeking in the video?<\/p>\n<p><kbd>defaultPlaybackRate*<\/kbd> <small>(float)<\/small>: The playback speed at which the video should be played<\/p>\n<p><kbd>playbackRate*<\/kbd> <small>(float)<\/small>: The current playback speed: 1.0 is normal, 2.0 is two times faster forward, -3.0 is three times faster backwards and so on<\/p>\n<p><kbd>seekable*<\/kbd> <small>(TimeRanges)<\/small>: An object of ranges in which the browser can seek (more about this below)<\/p>\n<p><kbd>buffered*<\/kbd> <small>(TimeRanges)<\/small>: An object of ranges which are already buffered<\/p>\n<p><kbd>played*<\/kbd> <small>(TimeRanges)<\/small>: An object of ranges which the user has already played<\/p>\n<h5>Other attributes<\/h5>\n<p><kbd>volume<\/kbd> <small>(float)<\/small>: The current volume &mdash; from 0.0 to 1.0<\/p>\n<p><kbd>muted<\/kbd> <small>(boolean)<\/small>: Is the video muted? Takes precedence of the volume<\/p>\n<p><kbd>readyState<\/kbd> <small>(int)<\/small>: State of the video (explanation below)<\/p>\n<p><kbd>networkState<\/kbd> <small>(int)<\/small>: State of the network (explanation below)<\/p>\n<p><kbd>error<\/kbd> <small>(MediaError)<\/small>: A media error object if something went wrong<\/p>\n<p>*: This attribute is currently not working in Firefox<\/p>\n<h4>Usage of the attributes<\/h4>\n<p>As you can imagine, some attributes as read-only (duration, ended, readyState) &#8211; some can be read and written, like volume, currentTime.<\/p>\n<p>You can access these attributes in Javascript like this:<\/p>\n<pre lang=\"javascript\">\r\nvar myvid = document.getElementById('vid');\r\nmyvid.muted = false;\r\nmyvid.currentTime = 0.0;\r\nif (myvid.ended) { ... }\r\n<\/pre>\n<h4>Error codes<\/h4>\n<p>If an error occured, you can read the error code to react &mdash; these error codes are currently supported:<\/p>\n<p><kbd>MEDIA_ERR_ABORTED (1)<\/kbd> User aborted video playback<\/p>\n<p><kbd>MEDIA_ERR_NETWORK (2)<\/kbd> Network error (could not read the stream)<\/p>\n<p><kbd>MEDIA_ERR_DECODE (3)<\/kbd> Decoding error, video is broken or the codec makes problems<\/p>\n<p><kbd>MEDIA_ERR_SRC_NOT_SUPPORTED (4)<\/kbd> The format is not supported<\/p>\n<p>Usage (you better use events therefore, but more of this later):<\/p>\n<pre lang=\"javascript\">\r\nvar myvid = document.getElementById('vid');\r\nif (myvid.error) {\r\n switch (myvid.error.code) {\r\n\tcase MEDIA_ERR_ABORTED:\r\n\t\talert(\"You stopped the video.\");\r\n\t\tbreak;\r\n\tcase MEDIA_ERR_NETWORK:\r\n\t\talert(\"Network error - please try again later.\");\r\n\t\tbreak;\r\n\tcase MEDIA_ERR_DECODE:\r\n\t\talert(\"Video is broken..\");\r\n\t\tbreak;\r\n\tcase MEDIA_ERR_SRC_NOT_SUPPORTED:\r\n\t\talert(\"Sorry, your browser can't play this video.\");\r\n\t\tbreak;\r\n }\r\n}\r\n<\/pre>\n<h4>Network and Ready states<\/h4>\n<p>These states are defined as follows (first the network then the ready states):<\/p>\n<p><kbd>NETWORK_EMPTY (0)<\/kbd> Not yet initialized<\/p>\n<p><kbd>NETWORK_IDLE (1)<\/kbd> Network is not being used now (video is completely loaded for example)<\/p>\n<p><kbd>NETWORK_LOADING (2)<\/kbd> Browser is loading data from the net<\/p>\n<p><kbd>NETWORK_LOADED (3)<\/kbd> Data has been loaded<\/p>\n<p><kbd>NETWORK_NO_SOURCE (4)<\/kbd> Video resource could not be found\/loaded<\/p>\n<p class=\"small\">The documentation of the network states is not correct on  most sites, so better refer to the states above.<\/p>\n<p><kbd>HAVE_NOTHING (0)<\/kbd> No data available<\/p>\n<p><kbd>HAVE_METADATA (1)<\/kbd> Duration and dimensions are available<\/p>\n<p><kbd>HAVE_CURRENT_DATA (2)<\/kbd> Data for the current position is available<\/p>\n<p><kbd>HAVE_FUTURE_DATA (3)<\/kbd> Data for the current and future position is available, so playback could start<\/p>\n<p><kbd>HAVE_ENOUGH_DATA (4)<\/kbd> Enough data to play the whole video is available<\/p>\n<h4>Events<\/h4>\n<p>To control the video playback we also need events, here a list of the provided events:<\/p>\n<p><kbd>loadstart<\/kbd> Browser starts loading data<\/p>\n<p><kbd>progress<\/kbd> Browser loads data<\/p>\n<p><kbd>suspend<\/kbd> Browser does not load data, waiting<\/p>\n<p><kbd>abort<\/kbd> Data loading was aborted<\/p>\n<p><kbd>error<\/kbd> An error occured<\/p>\n<p><kbd>emptied<\/kbd> Data not present unexpectedly<\/p>\n<p><kbd>stalled<\/kbd> Data transfer stalled<\/p>\n<p><kbd>play<\/kbd> Video started playing (fired with play())<\/p>\n<p><kbd>pause<\/kbd> Video has paused (fired with pause())<\/p>\n<p><kbd>loadedmetadata<\/kbd> Metadata loaded<\/p>\n<p><kbd>loadeddata<\/kbd> Data loaded<\/p>\n<p><kbd>waiting<\/kbd> Waiting for more data<\/p>\n<p><kbd>playing<\/kbd> Playback started<\/p>\n<p><kbd>canplay<\/kbd> Video can be played, but possibly must stop to buffer content<\/p>\n<p><kbd>canplaythrough<\/kbd> Enough data to play the whole video<\/p>\n<p><kbd>seeking<\/kbd> seeking is true (browser seeks a position)<\/p>\n<p><kbd>seeked<\/kbd> seeking is now false (position found)<\/p>\n<p><kbd>timeupdate<\/kbd> currentTime was changed<\/p>\n<p><kbd>ended<\/kbd> Video has ended<\/p>\n<p><kbd>ratechange<\/kbd> Playback rate has changed<\/p>\n<p><kbd>durationchange<\/kbd> Duration has changed (for streams)<\/p>\n<p><kbd>volumechange<\/kbd> Volume has changed<\/p>\n<p>So &#8211; lots of events and attributes as you can see. In order to let this not become too abstract, head to the <a href=\"\/blog\/2010\/02\/23\/html-5-video-test-area\/\">video test area<\/a>, there you can see the video attributes and fired events by trying out the buttons.<\/p>\n<p>Also a good read is this article from Dev.Opera: <a href=\"http:\/\/dev.opera.com\/articles\/view\/introduction-html5-video\/\" class=\"external\">dev.opera.com\/articles\/view\/introduction-html5-video &raquo;<\/a><\/p>\n<p>While this post is quite theoretical the <a href=\"\/blog\/2010\/03\/02\/html-5-video-mootools\/\">next one<\/a> <del datetime=\"2010-03-02T21:54:33+00:00\">(next week probably)<\/del> will be more hands-on &mdash; it will present some Javascript\/Mootools-code which lets you write your own video player controls.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The last last post was about the HTML 5 video tag, now we&#8217;ll have a look at the DOM side of things: Attributes and Events.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[8,14],"tags":[55,10,54,79],"class_list":["post-4232","post","type-post","status-publish","format-standard","hentry","category-webdevelopment","category-javascript","tag-javascript-mootools","tag-mootools","tag-track","tag-webdesign"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paPEN-16g","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/posts\/4232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/comments?post=4232"}],"version-history":[{"count":0,"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/posts\/4232\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/media?parent=4232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/categories?post=4232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/tags?post=4232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}