{"id":8481,"date":"2022-09-28T11:51:16","date_gmt":"2022-09-28T11:51:16","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=8481"},"modified":"2022-09-28T11:51:16","modified_gmt":"2022-09-28T11:51:16","slug":"audio-and-video-in-html5","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/09\/28\/audio-and-video-in-html5\/","title":{"rendered":"Audio and Video in HTML5"},"content":{"rendered":"\n<p>Earlier, native web technologies such as\u00a0HTML\u00a0didn&#8217;t allow embedding video and audio on the Web. Plugin-based technologies became popular for handling such content, but they had many problems, including not working well with HTML\/CSS\u00a0features, security and accessibility issues. Later,\u00a0HTML5\u00a0specification introduced such features with the\u00a0&lt;video>\u00a0and\u00a0&lt;audio>\u00a0elements.<\/p>\n\n\n\n<p>The &lt;audio&gt; element is used to embed audio files to a web page, and the &lt;video&gt; element is used to embed a video.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-add-audio-on-the-web-page-2\">How to Add Audio on the Web Page<\/h2>\n\n\n\n<p>Before HTML5, audio files were added to the page by integrating background sound with the help of &lt;bgsound> tag. The file was played while the page was viewed, and the user couldn\u2019t mute the sound. In HTML5, we can embed audio files using the &lt;audio> tag, and there is no need to connect third-party plugins. The audio element can be controlled with HTML or\u00a0Javascript\u00a0and styled with CSS.<\/p>\n\n\n\n<p>In the code, the&nbsp;src&nbsp;attribute refers to the URL of the audio file, and the&nbsp;controls&nbsp;attribute adds a control panel (launch button, scroll bar, volume regulator).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;audio src=\"name.ogg\" controls&gt;&lt;\/audio&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"audio-codecs-and-audio-file-formats-5\">Audio Codecs and Audio File Formats<\/h2>\n\n\n\n<p>As not all browsers support all audio formats, the audio file is encoded\/decoded with an audio codec (a digital electronic device or computer-based software application that aids in the compression and decompression of a digital audio data). All formats of audio files are added simultaneously via the\u00a0&lt;source>\u00a0element with the\u00a0src\u00a0attribute.<\/p>\n\n\n\n<p>When defining different file formats, we recommend to define\u00a0MIME-type\u00a0for each file, in order to let the browser localize supported file. The MIME-type is defined by the help of the\u00a0type\u00a0attribute.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-adding-different-formats-of-audio-files-7\">Example of adding different formats of audio files:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n &lt;head&gt;\n   &lt;title&gt;Title of the document&lt;\/title&gt;\n &lt;\/head&gt;\n &lt;body&gt;\n   &lt;audio controls&gt;\n     &lt;source src=\"\/build\/audios\/jingle_bells.ogg\" type=\"audio\/ogg\"&gt;\n     &lt;source src=\"\/build\/audios\/audio.mp3\" type=\"audio\/mpeg\"&gt;\n   &lt;\/audio&gt;\n   &lt;p&gt;Click the play button&lt;\/p&gt;\n &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>The most popular audio formats are the following ones:<\/p>\n\n\n\n<p><strong>MP3<\/strong>&nbsp;\u2013 the most popular audio format, which uses lossy compression and permits to reduce the file size. Despite the popularity among the users, TV companies and radio stations use more modern codecs ISO-MPEG, like AAC or MPEG-H.<\/p>\n\n\n\n<p><strong>AAC<\/strong>&nbsp;(Advanced Audio Codec) \u2014 closed codec, MP3 analog, but comparing to the latter, it provides higher quality with the same or stronger compression degree.<\/p>\n\n\n\n<p><strong>Ogg Vorbis<\/strong>\u2014 free format with an open code, supported in Firefox, Opera and Chrome. Provides good quality sound, but not sufficiently supported by device players.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-add-video-files-10\">How to Add Video Files<\/h2>\n\n\n\n<p>In previous version of HTML, videos were embedded into the site via third-party plugins, such as QuickTime, RealPlayer or Flash. HTML5 has a new\u00a0&lt;video>\u00a0tag, which is used to insert a video into the web page.<\/p>\n\n\n\n<p>In a code, it looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;video src=\"example.webm\" controls&gt;&lt;\/video&gt;<\/code><\/pre>\n\n\n\n<p>The&nbsp;src&nbsp;attribute indicates the URL of the file, and the&nbsp;controls&nbsp;attribute is used to display control elements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"video-codecs-and-video-file-formats-14\">Video Codecs and Video File Formats<\/h2>\n\n\n\n<p>Each browser supports particular codec, that\u2019s why, in order to provide video playback in all browsers, the video file must be placed in a few formats. Like in the case of audio files, all formats of video files are included in the &lt;source&gt; element, starting with the most preferred one. Every video file must have its MIME-type, which is defined by the&nbsp;type&nbsp;attribute.<\/p>\n\n\n\n<p>To make sure, that the browser can process video files, create a file .htaccess in the folder, where the web site is situated, which defines MIME-types for a video.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-adding-different-formats-of-video-files-16\">Example of adding different formats of video files:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;title&gt;Title of the document&lt;\/title&gt;\n    &lt;style&gt;\n      video {\n        width: 300px;\n        height: 220px;\n        border: 1px solid #666;\n      }\n    &lt;\/style&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;video controls&gt;\n      &lt;source src=\u201dhttp:\/\/techslides.com\/demos\/sample-videos\/small.ogv\u201d type=\"video\/ogg\"&gt;\n      &lt;source src=\"\/build\/videos\/arcnet.io(7-sec).mp4\" type=\"video\/mp4\"&gt;\n    &lt;\/video&gt;\n    &lt;p&gt;Some information about video&lt;\/p&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Today there are 3 basic video formats: MP4\/MPEG-4, OGG and WebM +. For video data compression and their playback, we use codecs.<\/p>\n\n\n\n<p>For a video file of the&nbsp;<strong>MPEG-4<\/strong>&nbsp;format, the \u041d.264 video codec and \u0410\u0410\u0421 audio codec are used. If you want to use codecs, you must get a license.<\/p>\n\n\n\n<p>For the&nbsp;<strong>Ogg<\/strong>&nbsp;video file, use the Theora video codec and the Vorbis audio codec with an open code.<\/p>\n\n\n\n<p>For the video files of the&nbsp;<strong>WebM +<\/strong>&nbsp;format, use the VP8 video codec and the Vorbis audio codec. In this case, a license is not required.<\/p>\n\n\n\n<p>Most servers don&#8217;t serve Ogg or mp4 media with the correct MIME types. For this, you may need to add the appropriate configuration.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>AddType audio\/ogg .oga\nAddType audio\/wav .wav\nAddType video\/ogg .ogv .ogg\nAddType video\/mp4 .mp4<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-add-subtitles-and-headings-20\">How to Add Subtitles and Headings<\/h2>\n\n\n\n<p>Subtitles and headings are added to audio and video files via the\u00a0&lt;track>\u00a0element, which is used as a child element of &lt;audio> and &lt;video>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-adding-subtitles-and-headings-to-audio-and-video-files-22\">Example of adding subtitles and headings to audio and video files:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;title&gt;Title of the document&lt;\/title&gt;\n    &lt;style&gt;\n      video {\n        width: 300px;\n        height: 200px;\n        border: 1px solid #666;\n      }\n    &lt;\/style&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;video controls muted src=\"\/build\/videos\/arcnet.io(7-sec).mp4\"&gt;\n      &lt;track default kind=\"subtitles\" srclang=\"en\" src=\"\/build\/videos\/arcnet.io(7-sec).mp4\" \/&gt;\n    &lt;\/video&gt;\n    &lt;p&gt;\n      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.\n    &lt;\/p&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>To align the video player on the page, place the &lt;video> element into the\u00a0&lt;div>\u00a0container, assign it a class, and then define the width and the height for it, corresponding to the size of your video.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"audio-and-video-attributes-25\">Audio and Video Attributes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Attribute<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>autoplay<\/td><td>Allows the audio\/video to automatically start playing while the rest of the page is loading.<\/td><\/tr><tr><td>autobuffer<\/td><td>Allows the audio\/video to automatically begin buffering.<\/td><\/tr><tr><td>controls<\/td><td>Allows controlling the audio\/video playback, including volume, pause\/resume playback.<\/td><\/tr><tr><td>loop<\/td><td>Allows the audio\/video to play again whenever it finishes.<\/td><\/tr><tr><td>muted<\/td><td>Makes the media play with the turned off sound by default.<\/td><\/tr><tr><td>preload<\/td><td>Specifies buffering large files. It can have one of these values:<\/td><\/tr><tr><td>&#8220;none&#8221;, which does not buffer the file.<\/td><\/tr><tr><td>&#8220;auto&#8221;, which buffers the media file.<\/td><\/tr><tr><td>&#8220;metadata&#8221;, which buffers only the metadata for the file.<\/td><\/tr><tr><td>src<\/td><td>The URL of the audio\/video to embed. It is optional.<\/td><\/tr><tr><td>poster<\/td><td>This is a URL of an image displayed before the video is played.<\/td><\/tr><tr><td>width<\/td><td>Specifies the display area width of the video, in CSS pixels.<\/td><\/tr><tr><td>height<\/td><td>Specifies the display area height of the video, in CSS pixels.<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Earlier, native web technologies such as\u00a0HTML\u00a0didn&#8217;t allow embedding video and audio on the Web. Plugin-based technologies became popular for handling such content, but they had many problems, including not working well with HTML\/CSS\u00a0features, security and accessibility issues. Later,\u00a0HTML5\u00a0specification introduced such features with the\u00a0&lt;video>\u00a0and\u00a0&lt;audio>\u00a0elements. The &lt;audio&gt; element is used to embed audio files to a web [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[425],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/8481"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=8481"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/8481\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=8481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=8481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=8481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}