HTML5 audio and video: Difference between revisions

The educational technology and digital learning wiki
Jump to navigation Jump to search
(Explained what a "fallback" is)
 
(20 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{incomplete}}
{{incomplete}}
{{web technology tutorial|Beginners}}
{{web technology tutorial|Beginners}}
<pageby nominor="false" comments="false"/>
<!-- <pageby nominor="false" comments="false"/> -->
== Introduction ==
== Introduction ==


Line 10: Line 10:
See also:
See also:
* [[HTML5]] (Overview)
* [[HTML5]] (Overview)
* [[HTML5 video and JavaScript]]
* [[Video editing and conversion]]. If your input type fits, we suggest using [http://firefogg.org/ FireOgg] for simple conversion (a Firefox plugin)
* [[WebVTT]] (a track file format that can be used with HTML5 Video Players)
* [[WebVTT]] (a track file format that can be used with HTML5 Video Players)


=== Browser support ===
=== Browser support ===
As of April 2011, so this may rapidly change. Also, there may be '''mistakes''', e.g. we did not test Safari and Android ourselves. In addition, the same browser may behave differently on different operating systems. Test it yourself, e.g. by using our simple [http://tecfa.unige.ch/guides/html/html5-video/html5-video-simple.html example file] ...). Or better, explore the links at the end of this article for more exhaustive and up-to-date information.


The most popular HTML5 video formats are webm/VP8, mp4/H.264 and ''OGG/Theora'' (ogv). Originally, the HTML5 specification wanted include OGG as standard, but after vendor opposition, it was decided to support all formats.
Browser support can change and there may be '''mistakes''' in the information below (updated March 2018). We did not test Safari and Android ourselves. In addition, a same browser may behave differently on different operating systems. Test it yourself, e.g. by using our simple [http://tecfa.unige.ch/guides/html/html5-video/html5-video-simple.html example file] ...). Or better, explore the links at the end of this article for more exhaustive and up-to-date information.


The following table provides an overview of implementations as of April 2012. It does not include browser extensions that will add extra functionality. See [http://leanbackplayer.com/other/compatibility_video.html LeanBackPlayer]'s or [http://en.wikipedia.org/wiki/HTML5_video Wikipedia's HTML5] for more detailed information.
The most popular HTML5 video formats are Webm/VP8/VP9), mp4/H.264 and ''OGG/Theora'' (ogv). Originally, the HTML5 specification wanted to include OGG as standard, but after vendor opposition, it was decided HTML5 should be open to accept all formats. As of 2018, most recent browers support most popular formats, i.e. WebM and MP4. Safari does not seem to play OGV (Ogg/Theora), the only free open source format.
 
The following table made in 2012 and updated March 2018 provides a summary overview. There may be mistakes and it does not include compatibility with all Codecs. It does not include browser extensions or that will add extra functionality, e.g. Flash. See [http://leanbackplayer.com/other/compatibility_video.html LeanBackPlayer]'s or [http://en.wikipedia.org/wiki/HTML5_video Wikipedia's HTML5 video] for more detailed information.
{| class="wikitable" style="text-align: center;"
{| class="wikitable" style="text-align: center;"
|+ Browser implementations of various Video formats
|+ Browser implementations of various Video formats
! Browser
! Browser
! colspan="3" | Formats natively supported by different web browsers (April 2012)
! colspan="3" | Formats natively supported by different web browsers (March 2018)
|-
|-
|
|
! style="width: 20%;" | ogv (Theora/Vorbis)
! style="width: 20%;" | ogv (Theora/Vorbis)
! style="width: 20%;" | mp4 (H.264/AVC
! style="width: 20%;" | mp4 (H.264/AVC)
! style="width: 20%;" | webm (VP8/Vorbis)
! style="width: 20%;" | webm (VP8/Vorbis)
|-
|-
! IE (9) || No || Yes || No
! IE (11) || No || Yes || No
|-
!Edge (17)
!Yes
!Yes
!Yes
|-
! Firefox (58) || Yes || yes || Yes
|-
! Chrome (49) || Yes || Yes || Yes
|-
|-
! Firefox (11) || Yes || no || Yes
!Chrome Android (64)
!Yes
!Yes
!Yes
|-
|-
! Chrome || Yes || Yes || Yes
! Safari 11 || No || Yes || No
|-
|-
! Safari || No || Yes || No  
!iOS Safari 10.2
!No
!Yes
!No
|-
|-
! Opera || Yes || No || Yes
! Opera || Yes || Yes || Yes
|-
|-
! Android || Yes || Yes || Yes
!Opera Mini
!No
!No
!No
|-
! Samsung Android || No || Yes || Yes
|}
|}


Line 45: Line 68:


Browser support for '''audio''', as for Video, is not uniform
Browser support for '''audio''', as for Video, is not uniform
{| style="text-align: center; class="wikitable"
{| style="text-align: center; class=" wikitable"
|+ Browser implementations of various audio formats
|+ Browser implementations of various audio formats
|-
|-
Line 56: Line 79:
! IE || no|| no || yes || yes  
! IE || no|| no || yes || yes  
|-
|-
! Firefox || yes || yes || no || no
! Firefox || yes || yes  
|yes
|-
|-
! Chrome || yes || yes || yes || yes  
! Chrome || yes || yes || yes || yes  
|-
|-
! Safari || ? || yes || yes || yes  
! Safari || no || yes || yes || yes  
|-
|-
! Opera || yes || yes || no || no  
! Opera mini || yes || yes || no || no  
|-
|-
! Android || ? || ? || yes || ?
! Android (Chrome, UC, Samsung) || yes || yes || yes || yes
|}
|}


Note: When you create a webpage and you wish to integrate video and sound using HTML5, you have to take into account the fact that people may be using browsers that don't support HTML5. Since your goal is to be sure your content is available to the widest possible audience, you have to provide an alternative.
Note about "fallback": When you create a webpage and you wish to integrate video and sound using HTML5, you have to take into account the fact that some people still may be using browsers that don't support HTML5. Since your goal is to be sure your content is available to the widest possible audience, you may provide an alternative.
This is called a "fallback": you either display a message stating that their browser doesn't support HTML5 (displaying ONLY a message is not a very good idea, since they won't be able to watch your video) or switch to a player that makes use of a more common (and older) technology such as an Adobe Flash player.
This is called a "fallback": you either display a message stating that their browser doesn't support HTML5 (displaying ONLY a message is not a very good idea, since they won't be able to watch your video) or switch to a player that makes use of a more common (and older) technology such as an Adobe Flash player.


== Basic use of video ==
== Basic use of video ==


While the video tag and its attributes are standardized, the video formats (i.e. containers and codecs) are not. For this reason, it is good practice to include several variants of the same file. Since the HTML5 video element doesn't work with older browsers and since default skin and controls differ among browser makers, you also may consider using an HTML5 video player library.
While the video tag and its attributes are standardized, the video formats (i.e. containers and codecs) are not, as explained above. For this reason, it is good practice to include several variants of the same file. As of 2018 most browsers do support popular video formats. However, since new formats are introduced every few years, a designer still must know about presenting alternatives. E.g. HEVC/H.265 only works in Microsoft and Apple Browsers as of March 2018. The former also require hardware support.
 
Since the HTML5 video element doesn't work with older browsers and since default skin and controls differ among browser makers, you also may consider using an HTML5 video player library.


Example file: http://tecfa.unige.ch/guides/html/html5-video/html5-video-simple.html (also look at its source code).
Example file: http://tecfa.unige.ch/guides/html/html5-video/html5-video-simple.html (also look at its source code).
Line 99: Line 125:
Sorry, your browser doesn't support HTML5 video
Sorry, your browser doesn't support HTML5 video
</video>
</video>
<p>This example wouldn't work in a browser like IE9  
<p>*.ogv - This example wouldn't work in a browser like IE9  
   that doesn't support Ogg</p>
   that doesn't support Ogg</p>


Line 105: Line 131:
Sorry, your browser doesn't support HTML5 video
Sorry, your browser doesn't support HTML5 video
</video>
</video>
<p>This example wouldn't work in a browser like Firefox  
<p>*.mp4 - This example wouldn't work in a browser like Firefox  
   that doesn't support mp4</p>
   that doesn't support mp4</p>


Line 111: Line 137:
Sorry, your browser doesn't support HTML5 video
Sorry, your browser doesn't support HTML5 video
</video>
</video>
<p>This example wouldn't work in a browser like IE9
<p>*.webm - This example wouldn't work in a browser like IE9
   that doesn't support webm</p>
   that doesn't support webm</p>
</source>
</source>
Line 171: Line 197:
</source>  
</source>  


Allowed '''video''' attributes:
The '''video''' element may have the following attributes:
* global attributes & autoplay & preload & controls & loop & poster & height & width & mediagroup & muted & src
 
: src — Address of the resource
: crossorigin — How the element handles crossorigin requests
: poster — Poster frame to show prior to video playback
: preload — Hints how much buffering the media resource will likely need
: autoplay — can be started automatically when the page is loaded
: playsinline — Encourage to display video content within the element's playback area
: loop — Whether to loop the media resource
: muted — Whether to mute the media resource by default
: controls — Show user agent controls
: width — Horizontal dimension
: height — Vertical dimension
 
Below we describe the most important ones.


;src = "source file"
;src = "source file"
Line 199: Line 238:


;width and height = "size"
;width and height = "size"
: Allow to define size of the video element
: Allow to define size of the video element in CSS pixels (mostly 96ppi)
: By default, width and height will adapt to the original size.
: By default, width and height will adapt to the original size.
: Example
: Example
Line 222: Line 261:


;preload = none | metadata | auto
;preload = none | metadata | auto
: Since most browsers will automatically start downloading the whole video, one could use this attribute to help the users save bandwith (in particular on mobile devices and slow home networks).
: This attribute tells whether and what to preload before the user clicks the play button. This will help the users save bandwidth which is particularly useful on mobile devices and slow home networks. Until recently (2018) some vendors did neither implement nor respect this hint.


: Example: The author hints that nothing should be downloaded.  
: Example: The author hints that nothing should be downloaded.  
Line 252: Line 291:
;style = "CSS style"
;style = "CSS style"
: CSS style properties
: CSS style properties
;mediagroup = "string with resources"
: Will tell the the client to link multiple video and audio streams
: Example (needed)


=== Web server configuration ===
=== Web server configuration ===
Line 331: Line 366:
</source>
</source>
There is no attribute for the volume, but there is JavaScript ...
There is no attribute for the volume, but there is JavaScript ...
Example:
* http://tecfa.unige.ch/guides/html/html5-audio/html5-audio-background.html


== The source element ==
== The source element ==


The '''source''' element must be either a child of the '''video''' or the '''audio''' element.
The '''source''' element must be either a child of the '''video''' or the '''audio''' element. Below we recall a simple example:
<source lang="HTML5">
<video id="movie1" controls>
  <source src="vid.mp4" />
</video>
</source>


It can include the so-called global attributes (like "style" or "id"). It's proper attributes are:
It can include the so-called global attributes (like "style" or "id"). It's proper attributes are:
Line 342: Line 385:


; type="MIME type"
; type="MIME type"
: Allows to identifies the mime type of the media source plus precise codes.
: Allows to identifies the mime type of the media source plus precise codecs.
: Examples
: Examples
<source lang="XML">
<source lang="XML">
Line 500: Line 543:
== Video capturing ==
== Video capturing ==


(to do ....)
[https://www.w3.org/TR/html-media-capture/ HTML Media Capture], allows to capture data from device capturing mechanism such as a microphone or the Camera. It has become a recommendation in Feb 2018. As of March 2018 it only seems to work in mobile browsers such as iOS Safari, Chrome for Android or Samsung Internet. There exist other technologies to do that and that rely on JavaScript.
 
Below we include the examples from the [https://www.w3.org/TR/html-media-capture/ 2018 recommendation], which also shows some JavaScript examples that we do not reproduce here.
 
Taking a picture
<source lang="HTML5">
<form action="server.cgi" method="post" enctype="multipart/form-data">
  <input type="file" name="image" accept="image/*" capture="user">
  <input type="submit" value="Upload">
</form>
</source>
 
Taking a video with the rear camera
<source lang="HTML5">
<form action="server.cgi" method="post" enctype="multipart/form-data">
  <input type="file" name="video" accept="video/*" capture="environment">
  <input type="submit" value="Upload">
</form>
</source>
 
Capture an audio
<source lang="HTML5">
<form action="server.cgi" method="post" enctype="multipart/form-data">
  <input type="file" name="audio" accept="audio/*" capture>
  <input type="submit" value="Upload">
</form>
</source>


== Links ==
== Links ==
Line 527: Line 596:
=== Tutorials ===
=== Tutorials ===


* [https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Video_and_audio_content Video and audio content] MDN Tutorial (2018), recommended.
* [http://www.w3.org/2010/Talks/1014-html5-video-fd/video-html5.pdf Towards Video on the Web with HTML5] by Daoust, Francois; Hoschka, Philipp; Patrikakis, Charalampos Z (2010)
* [http://www.w3.org/2010/Talks/1014-html5-video-fd/video-html5.pdf Towards Video on the Web with HTML5] by Daoust, Francois; Hoschka, Philipp; Patrikakis, Charalampos Z (2010)
* [https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox Using HTML5 audio and video], Mozilla (also useful for other browsers)
* [https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox Using HTML5 audio and video], Mozilla (also useful for other browsers)
Line 539: Line 609:


* [http://html5video.org/ HTML5Video.org]
* [http://html5video.org/ HTML5Video.org]
* [http://www.siteduzero.com/tutoriel-3-561857-la-video-et-l-audio.html/ Website in French]


=== Example code ===
=== Example code / testing ===


* http://playground.html5rocks.com/#video_element
* http://playground.html5rocks.com/#video_element
* [http://www.w3.org/2010/05/video/mediaevents.html MediaEvents Demo] (W3C)
* [http://www.w3.org/2010/05/video/mediaevents.html MediaEvents Demo] (W3C)
* [http://techslides.com/sample-webm-ogg-and-mp4-video-files-for-html5/ Sample WebM, Ogg, and MP4 Video Files for HTML5]


[[Category: Digital video]]
[[Category: Digital video]]
Line 549: Line 621:
[[Category: web standards]]
[[Category: web standards]]
[[Category: SVG]]
[[Category: SVG]]
[[fr:HTML5 audio et video]]

Latest revision as of 18:33, 26 March 2018

Introduction

This tutorial shows basic use of video and audio in HTML5. HTML5 includes special elements (tags) allowing to include video and audio and to define controls. Unlike in HTML 4.x and XHTML 1.x, video and audio is fully integrated in HTML5, meaning that these elements can also be styled and scripted via the DOM.

Before learning how to use video and audio in HTML5, you need to understand that media files are actually containers (i.e. so-called Multimedia container formats) that include in turn various types of data files, e.g. video and audio streams, chapter-information, captions (subtitles) and meta-information plus synchronization information. Audio and video data are compressed with so-called codecs (compression and decompression algorithms).

See also:

Browser support

Browser support can change and there may be mistakes in the information below (updated March 2018). We did not test Safari and Android ourselves. In addition, a same browser may behave differently on different operating systems. Test it yourself, e.g. by using our simple example file ...). Or better, explore the links at the end of this article for more exhaustive and up-to-date information.

The most popular HTML5 video formats are Webm/VP8/VP9), mp4/H.264 and OGG/Theora (ogv). Originally, the HTML5 specification wanted to include OGG as standard, but after vendor opposition, it was decided HTML5 should be open to accept all formats. As of 2018, most recent browers support most popular formats, i.e. WebM and MP4. Safari does not seem to play OGV (Ogg/Theora), the only free open source format.

The following table made in 2012 and updated March 2018 provides a summary overview. There may be mistakes and it does not include compatibility with all Codecs. It does not include browser extensions or that will add extra functionality, e.g. Flash. See LeanBackPlayer's or Wikipedia's HTML5 video for more detailed information.

Browser implementations of various Video formats
Browser Formats natively supported by different web browsers (March 2018)
ogv (Theora/Vorbis) mp4 (H.264/AVC) webm (VP8/Vorbis)
IE (11) No Yes No
Edge (17) Yes Yes Yes
Firefox (58) Yes yes Yes
Chrome (49) Yes Yes Yes
Chrome Android (64) Yes Yes Yes
Safari 11 No Yes No
iOS Safari 10.2 No Yes No
Opera Yes Yes Yes
Opera Mini No No No
Samsung Android No Yes Yes

The example video we are using is this page was taken from Vimeo. It includes a 2011 talk about the state of Wikipedia by Jimmy Wales, its founder. We produced several versions without paying a lot of attention to encoding details.


Browser support for audio, as for Video, is not uniform

Browser implementations of various audio formats
Ogg Vorbis (.ogg) WAV PCM (.wav) MP3 (.mp3) AAC (.m4a)
IE no no yes yes
Firefox yes yes yes
Chrome yes yes yes yes
Safari no yes yes yes
Opera mini yes yes no no
Android (Chrome, UC, Samsung) yes yes yes yes

Note about "fallback": When you create a webpage and you wish to integrate video and sound using HTML5, you have to take into account the fact that some people still may be using browsers that don't support HTML5. Since your goal is to be sure your content is available to the widest possible audience, you may provide an alternative. This is called a "fallback": you either display a message stating that their browser doesn't support HTML5 (displaying ONLY a message is not a very good idea, since they won't be able to watch your video) or switch to a player that makes use of a more common (and older) technology such as an Adobe Flash player.

Basic use of video

While the video tag and its attributes are standardized, the video formats (i.e. containers and codecs) are not, as explained above. For this reason, it is good practice to include several variants of the same file. As of 2018 most browsers do support popular video formats. However, since new formats are introduced every few years, a designer still must know about presenting alternatives. E.g. HEVC/H.265 only works in Microsoft and Apple Browsers as of March 2018. The former also require hardware support.

Since the HTML5 video element doesn't work with older browsers and since default skin and controls differ among browser makers, you also may consider using an HTML5 video player library.

Example file: http://tecfa.unige.ch/guides/html/html5-video/html5-video-simple.html (also look at its source code).

A note on self-closing tags and boolean attributes: The following code is perfectly legal HTML5:

<video id="movie1" controls>
  <source src="videos/state-of-wikipedia-480x272.mp4" >
  Your browser doesn't support HTML5. Maybe you should upgrade.
</video>

However, since there may be a future XHTML5 profile and since editors deal better with closed tags we suggest to make your code XML compliant:

<video id="movie1" controls="controls">
  <source src="videos/state-of-wikipedia-480x272.mp4" />
  Your browser doesn't support HTML5. Maybe you should upgrade.
</video>

Very simple use - not recommended

The following example code shows how to include a single video format. We will show three variants, one for each of popular the HTML5-supported formats. Including a single variant is not recommended. See below for defining alternatives.

<video src="videos/state-of-wikipedia-480x272.ogv" controls>
Sorry, your browser doesn't support HTML5 video
</video>
<p>*.ogv - This example wouldn't work in a browser like IE9 
   that doesn't support Ogg</p>

<video src="videos/state-of-wikipedia-480x272.mp4" controls>
Sorry, your browser doesn't support HTML5 video
</video>
<p>*.mp4 - This example wouldn't work in a browser like Firefox 
   that doesn't support mp4</p>

<video src="videos/state-of-wikipedia-480x272.webm" controls>
Sorry, your browser doesn't support HTML5 video
</video>
<p>*.webm - This example wouldn't work in a browser like IE9
   that doesn't support webm</p>

Simple use of alternatives

The following code shows how to define three alternatives with a fallback message for non HTML5 browsers.

<video id="movie1" controls>
  <source src="videos/state-of-wikipedia-480x272.mp4" />
  <source src="videos/state-of-wikipedia-480x272.ogv" />
  <source src="videos/state-of-wikipedia-480x272.webm" />
  Your browser doesn't support HTML5. Maybe you should upgrade.
</video>

The following code shows how to define alternatives that specify codecs. This method is useful if you plan to include "high quality" videos that some clients may not support.

<video id="movie2" controls="controls">
  <source src="videos/state-of-wikipedia-480x272.mp4" 
          type='video/mp4;codecs="avc1.42E01E, mp4a.40.2"' />
  <source src="videos/state-of-wikipedia-480x272.ogv"
          type='video/ogg; codecs="theora, vorbis"' />
  <source src="videos/state-of-wikipedia-480x272.webm"
          type='video/webm; codecs="vp8, vorbis"' />
	<p>The video is available as <a href="videos/state-of-wikipedia-480x272.mp4">H.264 in an MP4 container</a>, 
	or as <a href="videos/state-of-wikipedia-480x272.ogv">VP8 in a Ogg container</a>
	or as <a href="videos/state-of-wikipedia-480x272.webm">VP8 in a
	WebM container</a>. </p>
</video>

The HTML5 video element

Content model

  • The video element can not be used inside the a and the button elements
  • The video element must have both a start and an end tag. I.e. it can't be self-closed.
  • The video element either must include a single src attribute or one or more source elements followed by track elements, followed by any sort of or flow contents, i.e. all sorts of stuff, but that only will be shown as fallback).

Typical structure of a video element:

<video id="movie1" controls>
  <source src="vid.mp4" />
  <source src="..." />
  <track enabled="true" kind="subtitles" label="EN VTT" src="captions.vtt" srclang="en" type="text/vtt"/>
  Any sort of extra stuff below for fallback. E.g. a message like
  "Your browser doesn't support HTML5. Maybe you should upgrade."
</video>

Parameters of the video element

HTML5 attributes work like HTML4 attributes, i.e. so-called boolean attributes dont' need to include a value. In addition, it seems that attribute values don't need to be quoted in HTML5. The following three lines are strictly equivalent.

 controls
 controls="controls"
 controls=""

The video element may have the following attributes:

src — Address of the resource
crossorigin — How the element handles crossorigin requests
poster — Poster frame to show prior to video playback
preload — Hints how much buffering the media resource will likely need
autoplay — can be started automatically when the page is loaded
playsinline — Encourage to display video content within the element's playback area
loop — Whether to loop the media resource
muted — Whether to mute the media resource by default
controls — Show user agent controls
width — Horizontal dimension
height — Vertical dimension

Below we describe the most important ones.

src = "source file"
Allows to define a single video file. See the source element below for a better alternative.
Example:
 src="myfile.webm"
poster = "picture file"
Will display a picture while the video downloads and before the user decides to play the video. If absent, the first frame of the video will be shown.
Example:
 src="vaction_picture.jpg"
controls = on|off
Defines if use controls should be displayed. By default there will be none.
Examples:
controls
controls = "controls"
width and height = "size"
Allow to define size of the video element in CSS pixels (mostly 96ppi)
By default, width and height will adapt to the original size.
Example
 width="500"
 height="400"
autoplay = on|off
If present, the video will start playing automatically. This can be annoying since the user may not want to look at it.
Example
 autoplay
muted = on|off
If present, will mute the audio
Example
 muted
preload = none | metadata | auto
This attribute tells whether and what to preload before the user clicks the play button. This will help the users save bandwidth which is particularly useful on mobile devices and slow home networks. Until recently (2018) some vendors did neither implement nor respect this hint.
Example: The author hints that nothing should be downloaded.
 preload="none"
Example: Enough data should be downloaded to show a frame and to determine duration.
 preload="metadata"
Example: browser should download as much as it sees fit to give a good user experience, possibly downloading the whole video.
 preload=""
 preload="auto"
autobuffer (deprecated element, don't use this !)
If present, will start downloading the whole video after the page is loaded. Also use with care, because this will take up bandwidth.
 autobuffer
loop = on | off
If present, will loop
 loop
style = "CSS style"
CSS style properties

Web server configuration

Your web server must define all the mime types, else web browsers may not display video/audio.

In particular, either add the following mime types in the mime.types file

video/ogg ogg ogm ogv
video/mp4 mp4
video/webm webm
audio/mpeg mpga mp2 mp2a mp3 m2a m3a
audio/ogg oga ogg spx
audio/mp4 mp4a

On an Apache server, you also could edit an http.conf file like this:

AddType video/ogg .ogm
AddType video/ogg .ogv
AddType video/ogg .ogg
AddType video/mp4 .mp4
AddType video/webm .webm
AddType audio/mpeg mpga mp2 mp2a mp3 m2a m3a
AddType audio/ogg oga ogg spx
AddType audio/mp4 mp4a
AddType audio/playlist m3u M3U

If you don't have administrator rights, you could add them on a per directory basis, e.g. put the above list of AddTypes in the .htaccess file on Apache.

The audio element

The audio element is very similar to the video element. It includes the same content model (must not be within an a or a button) and can include source elements, followed by track elements followed by flow elements or phrasing content.

Permitted attributes are mostly the same:

  • global attributes & autoplay & preload & controls & loop & mediagroup & muted & src
  • Audio doesn't support the poster attribute (wouldn't make sense).


Example file: http://tecfa.unige.ch/guides/html/html5-audio/html5-audio-simple.html (also look at its source code).

Simple use with control

Ogg

<audio src="audio/guitar.ogg" controls>
  Sorry, your browser doesn't support HTML5 audio
</audio>

MP3

<audio src="audio/guitar.mp3" controls>
  Sorry, your browser doesn't support HTML5 audio
</audio>

Alternatives

<audio id="audio1" controls>
  <source src="audio/guitar.mp3">
  <source src="audio/guitar.ogg">
  <source src="audio/guitar.m4a">
  <source src="audio/guitar.wav">
Your browser doesn't support HTML5. Maybe you should upgrade.
</audio>

Simple background music

<audio id="my_song" autoplay loop>
 <source src="audio/guitar.mp3">
 <source src="audio/guitar.ogg">
 <source src="audio/guitar.m4a">
 <source src="audio/guitar.wav">
 Your browser doesn't support HTML5. Maybe you should upgrade.
</audio>

There is no attribute for the volume, but there is JavaScript ...

Example:

The source element

The source element must be either a child of the video or the audio element. Below we recall a simple example:

<video id="movie1" controls>
  <source src="vid.mp4" />
</video>

It can include the so-called global attributes (like "style" or "id"). It's proper attributes are:

src ="URL" (mandatory)
Identifies the video file
type="MIME type"
Allows to identifies the mime type of the media source plus precise codecs.
Examples
 type='video/mp4;codecs="avc1.42E01E, mp4a.40.2"'
 type="video/mp4;codecs='avc1.42E01E, mp4a.40.2'"
media
Can define a media type to help the client selecting the right video source.

The track element

The track element allows to add captions (subtitles) and other information, i.e. allow to add textual information that can be displayed to the user. However, track currently (April 2012) is not implemented in most browsers. In addition, the supported track file formats still seem to be a very moving target. Contenders are WebVTT, SRT and Timed Text (TTML). In addition, some containers also may include their own "track" data.

Example code found at Mozilla. Tracks may not yet work in your browser.

<video src="sample.ogv">
   <track kind="captions" src="sampleCaptions.srt" srclang="en">
   <track kind="descriptions" src="sampleDesciptions.srt" srclang="en">
   <track kind="chapters" src="sampleChapters.srt" srclang="en">
   <track kind="subtitles" src="sampleSubtitles_de.srt" srclang="de">
   <track kind="subtitles" src="sampleSubtitles_en.srt" srclang="en">
   <track kind="subtitles" src="sampleSubtitles_ja.srt" srclang="ja">
   <track kind="subtitles" src="sampleSubtitles_oz.srt" srclang="oz">
   <track kind="metadata" src="keyStage1.srt" srclang="en" label="Key Stage 1">
   <track kind="metadata" src="keyStage2.srt" srclang="en" label="Key Stage 2">
   <track kind="metadata" src="keyStage3.srt" srclang="en" label="Key Stage 3">
</video>

Good HTML5 players, however implement some of these track formats.

Simple live example using the LeanBack Player

<div class="leanback-player-video">
  <video controls="controls" preload="metadata" poster="wikipedialogo.png" width="480" height="272">
   <source src="videos/state-of-wikipedia-480x272.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
   <source src="videos/state-of-wikipedia-480x272.webm" type='video/webm; codecs="vp8, vorbis"' />
   <source src="videos/state-of-wikipedia-480x272.ogv" type='video/ogg; codecs="theora, vorbis"' />
	
   <track enabled="true" kind="subtitles" label="EN"
          src="subtitles_en.srt" srclang="en" type="text/x-srt"/>
   <track enabled="true" kind="subtitles" label="EN VTT"
          src="subtitles_en.vtt" srclang="en" type="text/vtt"/>
   <track enabled="true" kind="subtitles" label="FR" 
          src="subtitles_fr.vtt" srclang="fr" type="text/vtt"/>
   <object class="leanback-player-flash-fallback" width="640" height="360"
           type="application/x-shockwave-flash"
           data="http://releases.flowplayer.org/swf/flowplayer.swf">
      <param name="movie" value="http://releases.flowplayer.org/swf/flowplayer.swf" />
      <param name="allowFullScreen" value="true" />
      <param name="wmode" value="opaque" />
      <param name="bgcolor" value="#000000" />
      <param name="flashVars" 
             value="config={'playlist':['wikipedialogo.png', 
		{'url':'videos/state-of-wikipedia-480x272.mp4','autoPlay':false,'autobuffering':false}]}" />
   </object>
   <div class="leanback-player-html-fallback" style="width: 640px; height: 360px;">
     <img src="wikipedialogo.png" width="640" height="360" alt="Poster Image" 
          title="No HTML5-Video playback capabilities found. Please download the video(s) below." />
    <div>
<strong>Download Video:</strong>
   <a href="videos/state-of-wikipedia-480x272.mp4">.mp4</a>
   <a href="videos/state-of-wikipedia-480x272.webm">.webm</a>
   <a href="videos/state-of-wikipedia-480x272.ogv">.ogv</a>
 </div>
</div>
</div>

Live example file:

Track files:

Track attributes

kind
Kind of text track. The following keywords are allowed according to Mozilla:
subtitles: A transcription or translation of the dialogue.
captions: A transcription or translation of the dialogue or other sound effects. Suitable for users who are deaf or when the sound is muted.
descriptions: Textual descriptions of the video content. Suitable for users who are blind or not able to view the video for another reason.
chapters: Chapter titles, intended to be used when the user is navigating the media resource.
metadata: Tracks used by script. Not visible to the user.
src (mandatory)
Defines the URL for the track file
srclang
Defines the language of the track text, so that the user and/or the client can make a choice
label
A user-readable title of the text track Used by the browser when listing available text tracks.
default
Identifies a track as default track. It only can be used with one track (of course).

Style and controlling media playback

In addition to using the width and height attribute for the video tag, you can use any reasonable and not reasonable CSS properties.

<video id="movie1" controls preload="metadata" 
          style="float:right;
                 margin: 0px 10px 0px 10px;
                 border-style: solid;
                 border-width: 50px 50px 50px 50px;
                 border-image: url(bricks.jpg) 50 round;
                 -webkit-border-image: url(bricks.jpg) 50 round;
                 -moz-border-image: url(bricks.jpg) 50 round;
                 ">
 <source src="videos/state-of-wikipedia-480x272.mp4">
 <source src="videos/state-of-wikipedia-480x272.ogv">
 <source src="videos/state-of-wikipedia-480x272.webm">
 Your browser doesn't support HTML5. Maybe you should upgrade.
</video>

Users can control playback through the default player. However, in case you don't want to use the control attribute, you can implement your own through JavaScript. An other alternative is to work with a Player library (see below).

Below is an audio control example found in the Using audio in video in Firefox tutorial

<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>

HTML5 player libraries

Since default controls in navigators do not offer a lot of functionality and since there still many non-HTML5 navigators installed, so-called HTML5 video players can offer both advanced controls and fallback. In addition they may implement features that are still missing in various browser implementations. These HTML5 video players are implemented with ready-to-go JavaScript libraries. Of course, you could write your own.

In addition, web designers may not like the visual aspect of default player and may want to adapt it to the page design.

These players are implemented with JavaScript, offer different functionality and may or may not be easy to use. More precisely, this principle can be explained by example: “An HTML5 Video Player is a JavaScript library that builds a custom set of controls over top of the HTML5 video element to provide a consistent look between HTML5 browsers. Video.js builds on this by fixing many cross browser bugs or inconsistencies, adding new features that haven't been implemented by all browsers (like fullscreen and subtitles), as well as providing one consistent JavaScript API for both HTML5, Flash, and other playback technologies.” (What's an HTML5 Video Player, retrieved 13:36, 22 April 2012 (CEST) from vidojs.com). Other player products could be described in the same way.

Some of HTML5 players (in particular order) are:

HTML5 Video.org offers a HTML5 Player Comparison, i.e. provides an overview table plus a detailed description of each HTML5 Player library. Recommended reading, although it may be biased since it is sponsored by Kaltura.

Otherwise, you can search for something like "best html5 player", but pay attention to the publication date !

Video capturing

HTML Media Capture, allows to capture data from device capturing mechanism such as a microphone or the Camera. It has become a recommendation in Feb 2018. As of March 2018 it only seems to work in mobile browsers such as iOS Safari, Chrome for Android or Samsung Internet. There exist other technologies to do that and that rely on JavaScript.

Below we include the examples from the 2018 recommendation, which also shows some JavaScript examples that we do not reproduce here.

Taking a picture

<form action="server.cgi" method="post" enctype="multipart/form-data">
  <input type="file" name="image" accept="image/*" capture="user">
  <input type="submit" value="Upload">
</form>

Taking a video with the rear camera

<form action="server.cgi" method="post" enctype="multipart/form-data">
  <input type="file" name="video" accept="video/*" capture="environment">
  <input type="submit" value="Upload">
</form>

Capture an audio

<form action="server.cgi" method="post" enctype="multipart/form-data">
  <input type="file" name="audio" accept="audio/*" capture>
  <input type="submit" value="Upload">
</form>

Links

Specifications

  • WebRTC 1.0: Real-time Communication Between Browsers, W3C Editor's Draft 16 March 2012. This specification defines a set of APIs to represent streaming media, including audio and video, in JavaScript, to allow media to be sent over the network to another browser or device implementing the appropriate set of real-time protocols, and media received from another browser or device to be processed and displayed locally.

Manuals and reference

Tutorials

Web sites

Example code / testing