Category: Media

  • Embed Multimedia

    In the previous two chapters, we have used the <audio> and <video> elements to add music and videos into our web page. There are other alternative ways to add videos, sounds, images, or any other external content to the website by using HTML tags <embed>and<object>. These tags cause the browser itself to include controls for the multimedia automatically:

    • HTML <embed> tag is used to embed external content such as images, videos, and web applications. It is often used for embedding content like Flash movies or audio/video players.
    • HTML <object> tag is used to embed various types of external resources, including images, videos, PDFs, and other web resources. It can render multiple types of files.

    Syntax

    Here is the syntax of embedding multimedia in the webpage:Embed tag:

    <embed src = "url_of_resource">

    Object tag:

    <object data="url_of_resource" type="typeOfResource">

    Attributes of <embed> Tag

    The following are the attributes used with the <embed> tag:

    AttributeDescription
    widthWidth attribute is used describe width of the embedded file in pixels.
    heightHeight of the embedded file in pixels.
    titleIt is used to label the content. The title should describe the content.
    srcURL of the file to be embedded.
    typeIt indicates the type of input like mp4 and mp3.

    Attributes of <object> Tag

    The following are the attributes used with the <object> tag:

    AttributesDescription
    dataThe location or path of the resource is passed into data attribute.
    typeIt indicates the type of resource.
    heightIt signifies the height of the resource display area.
    widthIt signifies the width of the resource display area.
    formIts value is the id of a form. The form attribute shows which object is associated with the form.
    nameIt specify a unique name for the object.
    usemapSpecifies a URL of a client-side image map to be used with the object.

    Examples of HTML Multimedia Embedding

    Here are a few examples that illustrate how to render multimedia content in a webpage using the <embed> and <object> tags:

    • Embedding a Video Using <embed> Element
    • Embedding an Audio Using <embed> Element
    • Render a PDF Using <object> Element
    • Render an HTML Document Inside Webpage

    Embedding a Video Using <embed> Element

    To embed an external video file inside the web page, we can pass the path of the video file into the src attribute of the <embed> element. The supported video formats are MP4, WebM, and Ogg. We dont need to use the controls attribute, as the <embed> tag provides the controls automatically depending on the type of media. The controls attribute allows users to manage the video playback functions.

    The following example demonstrates how to embed a video file using the embed element:

    <!DOCTYPE html><html><head><title>HTML embed Tag</title></head><body><h1>Playing video using embed tag</h1><embed src="/html/media/video/sampleTP.mp4" 
    
          type="video/mp4" 
          width="450" 
          height="250"&gt;&lt;/embed&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Embedding an Audio Using <embed> Element

    To add a soundtrack to the webpage, we can pass the path of the audio file into the src attribute by mentioning the type of audio. The supported audio formats are ogg, mp3, and wav.

    The following example demonstrates how to embed an audio file using the <embed> element:

    <!DOCTYPE html><html><head><title>HTML embed Tag</title></head><body><h1>Playing audio using embed tag</h1><embed src = "/html/media/audio/sample_3sec_audio.mp3" 
    
          type = "audio/mp3" 
          width="450" 
          height="250"&gt;&lt;/embed&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Render a PDF Using <object> Element

    HTML 4 introduces the <object> element, which offers an all-purpose solution to generic object inclusion. The <object> element allows HTML authors to specify everything required by an object for its presentation by a user agent.

    We can embed a PDF document in an HTML document as follows:

    <!DOCTYPE html><html lang="en"><head><title>PDF Embed Example</title></head><body><h1>Embedding a PDF Document</h1><p>Here is an embedded PDF document:</p><object data="html/pdf/index.pdf" 
    
              type="application/pdf" 
              width="300" 
              height="200"&gt;&lt;/object&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Render an HTML Document Inside Webpage

    We can embed an HTML document in an HTML document itself as follows:

    <!DOCTYPE html><html lang="en"><head><title>HTML Embed Example</title></head><body><h1>Embedding an HTML Document</h1><p>Here is an embedded HTML document:</p><object data="html/index.htm" 
    
            type="text/html" 
            width="500" 
            height="400"&gt;
      alt : &lt;a href="html/index.htm"&gt;
         test.htm
      &lt;/a&gt;&lt;/object&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Comparison between <object> and <embed> Tags

    Comparison between two similar tags will help to choose the right tag on the right place.

    Feature<object><embed>
    HTML Tag<object><embed>
    UsageEmbeds multimedia like audio, video, Java applets, ActiveX, PDF, and Flash.Primarily used to embed multimedia content
    AttributesSupports various attributes like data, type, width, and height.Supports various attributes like src, type, width, and height.
    HTML5SupportedSupported
    Fallback ContentCan include fallback content within the tagCannot include fallback content within the tag

    The object tag supports fallback content, which means if a version of a browser does not support the object tag, then the content written inside the object tag is displayed instead of the object tag.

  • Audio Element

    The HTML <audio> element embeds an audio file to the webpage. You can add an audio player inside a webpage using the <audio> element.

    HTML <audio> Element

    The <audio> element is used to enable the support of audio files within a web page. We can include multiple sources of audio; however, the browser will choose the most appropriate file automatically. Most of the attributes of <video> element is also compatible with the <audio> element. The most frequently used attributes of the HTML audio element are controlsautoplayloopmuted, and src.

    Attributes of <audio> Elements

    The following are the attributes used with the <audio> tag:

    AttributeDescription
    controlsThis attribute adds built-in audio controls for play, pause, and volume.
    autoplayThis attribute allows playing the audio automatically when the page is loaded.
    loopThis attribute allows looping of the audio.
    mutedThis attribute mutes the audio by default when the page is loaded.
    preloadThis attribute specifies how the audio should be preloaded by the browser.
    srcThis attribute specifies the path to the audio file.File URL

    Embedding an Audio in HTML

    You can embed an audio player using the <audio> tag by specifying the audio file path. The audio file path can be defined either by setting the src attribute or by including the <source> tag.

    The current HTML5 draft specification does not specify which audio formats browsers should support in the audio tag. But the most commonly used audio formats are oggmp3, and wav. Therefore, it is also possible to supply all these formats by using multiple <source> tags within the <audio> element. Let the browser decide which format is more suitable for audio playback.

    It is necessary to use the controls attribute so that users can manage audio playback functions such as volume adjustment, forward or backward navigation, and play or pause operations.

    Syntax

    Here is the syntax to embed an audio file:

    <audio controls><source src="file_path" type="audio/mpeg">
      Your browser does not support the audio element.
    </audio>

    Here,

    • <audio>: The main element to embed an audio player.
    • controls: Controls to add play, pause, and volume functionalities.
    • <source>: It specifies the audio file name (along with its path) and the audio file’s format.
    • Fallback text: The text to be displayed if the browser doesn’t support the <audio> element. In the above syntax, it will display:
      “Your browser does not support the audio element.”

    Example of HTML Audio Element

    Here is the complete example you can use to embed an audio player on the webpage:

    <!DOCTYPE html><html><body><p>Working with audio element</p><audio controls><source src= "/html/media/audio/sample_3sec_audio.mp3" type = "audio/mp3" /><source src= "/html/media/audio/sample_3sec_audio.wav" type = "audio/wav" /><source src= "/html/media/audio/sample_3sec_audio.ogg" type = "audio/ogg" />
    
      Your browser does not support the &lt;audio&gt; element.
    </audio></body></html>

    Using autoplay, muted, and loop Attributes in Audio Player

    We can also configure the audio to play automatically in a loop by using the autoplay and loop attributes. Remember, the Chrome browser does not support the autoplay feature unless the muted attribute is used. Therefore, it is recommended to use autoplay and muted attributes together.

    The autoplay is a Boolean attribute that is used to play the audio automatically once the page is loaded. If the loop attribute is present on the <audio> element, the audio sound will automatically rewind to the beginning once the end of the audio is reached.

    Example

    The following example illustrates the autoplay and looping of audio −

    <!DOCTYPE html><html><body><p>Working with audio element</p><audio controls autoplay muted loop><source src= "/html/media/audio/sample_3sec_audio.mp3" type = "audio/mp3" /><source src= "/html/media/audio/sample_3sec_audio.wav" type = "audio/wav" /><source src= "/html/media/audio/sample_3sec_audio.ogg" type = "audio/ogg" />
    
      Your browser does not support the &lt;audio&gt; element.
    </audio></body></html>

    Browser Support for Different Audio Formats

    The table below illustrates the various audio formats that are supported by different browsers:

    BrowserOGGWAVMP4
    ChromeYesYesYes
    EdgeYesYesYes
    SafariNoYesYes
    FirefoxYesYesYes
    OperaYesYesYes

  • Video Elements

    The HTML <video> element embeds and shows a video on the webpage. You can embed any type of video content on the webpage by using the <video> element.

    HTML <video> Element

    The <video> element is used to enable video playback support within a web page. It works very similarly to the <img> element, as it also requires adding the path or URL of the video within the src attribute. The HTML supports only MP4, WebM, and Ogg video formats. The <video> element also supports audio; however, the <audio> element is more suitable for that purpose.

    Embedding Videos in HTML

    To embed a video inside a web page, you need to set the src attribute inside the <video> tag that specifies the path or URL for the video. A web page serves content to a wide variety of users with various types of browsers. Each browser supports different video formats (MP4, WebM, and Ogg) as mentioned earlier. Therefore, we can supply all the formats that HTML supports by including multiple <source> tags. Let the browser decide which format is more suitable for video playback.

    Syntax

    Below is the syntax to embed a video on the webpage:

    <video width="640" height="360" controls><source src="video-file.mp4" type="video/mp4"><source src="video-file.ogg" type="video/ogg">
      Your browser does not support the video tag.
    </video>

    The controls Attribute

    You can also enable the built-in controls for controlling audio and video playback for the users (if needed) with the help of the controls attribute. It provides an interface that enables users to manage video playback functions such as volume adjustment, video navigation (forward and backward), and play or pause operations.

    Example to Embed a Video

    The following example shows how to insert the path or URL of a video inside the video element:

    <!DOCTYPE html><html><head><title>HTML Video Element</title></head><body><p>Playing video using video element</p><p>The browser is responsible for determining the appropriate format to use.</p><video width="450" height="250" controls><source src="/html/media/video/sampleTP.webm" type="video/webm"><source src="/html/media/video/sampleTP.mp4" type="video/mp4"><source src="/html/media/video/sampleTP.ogv" type="video/ogg"><p>Sorry, video element is not supported!</p></video></body></html>

    Customizing Video Display Size

    To set (adjust) the dimensions of the video display area, also known as the viewport, you can use the height and width attributes of the <video> element. These attributes define the height and width of the video viewport in pixels.

    Note that the video will adjust its aspect ratio (e.g., 4:3 and 16:9) to align with the specified height and width. Therefore, it is advisable to match the dimensions of the viewport for a better user experience.

    Example

    Let’s look at an example code below to understand better:

    <!DOCTYPE html><html><head><title>HTML Video Element</title></head><body><p>Playing video using video element</p><video width="450" height="250" controls><source src="/html/media/video/sampleTP.mp4" type="video/mp4"></video></body></html>

    HTML Video autoplay and loop Attributes

    You can configure the video to play automatically in a loop by using the autoplay and loop attributes.

    Remember, a few browsers like Chrome 70.0 do not support the autoplay feature unless the muted attribute is used. Therefore, it is recommended to use autoplay and muted attributes together.

    Example

    The following example illustrates the autoplay and looping of video:

    <!DOCTYPE html><html><head><title>HTML Video Element</title></head><body><p>Playing video using video element</p><video width="450" height="250" autoplay muted loop><source src="/html/media/video/sampleTP.mp4" type="video/mp4"></video></body></html>

    Setting a Video Thumbnail

    You can pass a URL of an image that works as a thumbnail for the video within the poster attribute of the <video> element. It will display the specified image until the video starts playing.

    Example

    In the following example, we are illustrating the use of poster attribute:

    <!DOCTYPE html><html><head><title>HTML Video Element</title></head><body><p>Playing video using video element</p><video width="450" height="250" controls muted poster ="tutorials_point.jpg" ><source src="/html/media/video/sampleTP.mp4" type="video/mp4"></video></body></html>

    Browser Support for Various Video Formats

    The table below illustrates the various video formats that are supported by different browsers:

    BrowserOGGWebMMP4
    ChromeYesYesYes
    EdgeYesYesYes
    SafariNoYesYes
    FirefoxYesYesYes
    OperaYesYesYes