Use HTML5 to embed audio and video playback in webpages.

Source: Internet
Author: User

Use HTML5 to embed audio and video playback in webpages.

HTML5 features, including native audio and video support without Flash.

The HTML5 <audio> and <video> tags make it easy to add media to the site. We only need to set the src attribute to identify media resources, including the controls attribute so that users can play and pause the media.

Embedded Video
The following is the simplest form of embedding a video file on a Web page:

Copy XML/HTML Code to clipboard
  1. <Video src = "foo.mp4" width = "300" height = "200" controls>
  2. Your browser does not support the <video> element.
  3. </Video>

Currently, the draft HTML5 specification does not specify the video format that the browser should support in the video tag. However, the most common video formats are:

Ogg: Ogg file with Thedora Video Encoder and Vorbis Audio Encoder.
Mpeg4: MPEG4 files with H.264 Video Encoder and AAC audio encoder.
You can use the <source> label with media types and other attributes to specify a media file. The video element allows multiple source elements. The browser uses the first accepted format:

Copy XML/HTML Code to clipboard
  1. <! Doctype html>
  2. <Html>
  3. <Body>
  4. <Video width = "300" height = "200" controls autoplay>
  5. <Source src = "/html5/foo.ogg" type = "video/ogg"/>
  6. <Source src = "/html5/foo.mp4" type = "video/mp4"/>
  7. Your browser does not support the <video> element.
  8. </Video>
  9. </Body>
  10. </Html>

Video attribute Specification
HTML5 video tags can use multiple attributes to control the appearance and feeling as well as various control functions:

Attribute Description
Autoplay If this Boolean attribute is specified, the video starts to play automatically immediately as long as the data is not stopped.
Autobuffer If this Boolean attribute is specified, the video is automatically buffered even if automatic playback is not set.
Controls If this attribute is specified, the user is allowed to control video playback, including volume control, fast forward, pause, or Resume playback.
Height This attribute specifies the height of the video display area in the form of CSS pixels.
Loop If this Boolean attribute is specified, automatic playback is allowed after the playback ends.
Preload Specify this attribute. The video will be loaded and ready when loading the page. If automatic playback is specified, this parameter is ignored.
Poster This is an image URL that is displayed to the user for playback or fast forward.
Src The URL of the video to be embedded. Optional. You can use the <source> element in the video block to specify the video to be embedded.
Width This attribute specifies the width of the video display area in the form of CSS pixels.

Embedded audio
HTML5-supported <audio> labels are used to embed voice content in the following HTML or XHTML documents.

Copy XML/HTML Code to clipboard
  1. <Audio src = "foo.wav" controls autoplay>
  2. Your browser does not support the <audio> element.
  3. </Audio>

The current HTML draft specification does not specify the audio format that the browser should support in the audio tag. However, the most common audio formats are ogg, mp3, and wav.

You can use the <source> tag with media types and other attributes to specify a media set. The Audio element allows multiple source elements, and the browser uses the first accepted format:

Copy XML/HTML Code to clipboard
  1. <! Doctype html>
  2. <Html>
  3. <Body>
  4. <Audio controls autoplay>
  5. <Source src = "/html5/audio.ogg" type = "audio/ogg"/>
  6. <Source src = "/html5/audio.wav" type = "audio/wav"/>
  7. Your browser does not support the <audio> element.
  8. </Audio>
  9. </Body>
  10. </Html>

Audio attribute Specification

The HTML5 audio tag can use multiple attributes to control the appearance, feeling, and various control functions:

Attribute Description
Autoplay If this Boolean attribute is specified, the audio will be automatically played immediately as long as the data loading is not stopped.
Autobuffer If this Boolean attribute is specified, audio buffering starts automatically even if automatic playback is not set.
Controls If this attribute is specified, the user is allowed to control audio playback, including volume control, fast forward, and pause/Resume playback.
Loop If this Boolean attribute is specified, the audio can be automatically played back after the playback ends.
Preload This attribute specifies that the page is loaded and ready. If automatic playback is specified, this parameter is ignored.
Src The URL of the audio to be embedded. Optional. You can use the <source> element in the audio block to specify the audio to be embedded.

Process media events
HTML5 audio and video tags can use multiple attributes to control various control functions using JavaScript:
Event Description
Abort This event is generated when the playback is aborted.
Canplay This event is generated when enough data is available and the media can play back.
Ended This event is generated when playback is complete.
Error This event is generated when an error occurs.
Loadeddata This event is generated when the first frame of the media is loaded.
Loadstart This event is generated when you start loading media.
Pause This event is generated when the playback is paused.
Play This event is generated when the playback starts or resumes.
SS This event is generated when media download progress is regularly notified.
Ratechange This event is generated when the playback speed changes.
Seeked This event is generated when the fast forward operation is complete.
Seeking This event is generated at the beginning of the fast forward operation.
Suspend This event is generated when media loading is paused.
Volumechange This event is generated when the audio volume changes.
Waiting Request operations (such as playback) are delayed. This event is generated when another operation is completed (such as fast forward.

The following is an example of a video that can be played:

Copy XML/HTML Code to clipboard
  1. <! Doctype html>
  2. <Head>
  3. <Script type = "text/javascript">
  4. Function PlayVideo (){
  5. Var v = document. getElementsByTagName ("video") [0];
  6. V. play ();
  7. }
  8. </Script>
  9. </Head>
  10. <Html>
  11. <Body>
  12. <Form>
  13. <Video width = "300" height = "200" src = "/html5/foo.mp4">
  14. Your browser does not support the <video> element.
  15. </Video>
  16. <Input type = "button" onclick = "PlayVideo ();" value = "Play"/>
  17. </Form>
  18. </Body>
  19. </Html>

Configure the server media type
Most servers do not use the correct MIME type to provide Ogg or mp4 media by default, so we may need to add appropriate configurations.


The Code is as follows: AddType audio/ogg. oga
AddType audio/wav. wav
AddType video/ogg. ogv. ogg
AddType video/mp4. mp4

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.