How to use video video and audio audio in HTML5. The corresponding data is stored locally in HTML5

13 minutes
three hundred and forty-seven
zero

This article mainly explains the built-in and elements in HTML5, which can be easily embedded into multimedia pages.

Embedded Media

It is very convenient to use HTML5 to write web pages and embed media in your web pages:

Your browser does not support the video element.

This example is to load a video in a webpage. Here is an example of embedding audio in your HTML document:

Your browser does not support the audio element.

The src attribute can be the remote URL address of an audio file or the local address of your website.

Your browser does not support the audio element

This code example uses the attributes of the element:

Controls: Whether to display the audio standard HTML5 control on the web page. Autoplay: whether to automatically play loop: the number of cycles, which is only played once by default, is set to a negative number for infinite cycles.

The preload attribute is used to buffer media files and provides three parameters.

"None" does not buffer files "auto" buffers the media file "metadata" buffers only the metadata for the file

Multiple source files can use components to provide provisions for encoding video or audio in different formats in different browsers. For example:

Your browser does not support the video element.

This ogg file needs the browser to support Ogg format. If the browser does not support ogg, the browser uses MPEG-4 files. See Media Format Support for Audio and Video Elements in Different Browsers.

You can also specify the media file requirements of the codec; This allows browsers to make more informed decisions:

Your browser does not support the video element.

Here, the video we specify uses Dirac and Speedx codecs. If the browser supports ogg, but no codec is specified, the video will not be loaded.

If the type attribute is not specified, the media type can be retrieved from the server and viewed by the browser. If it is not recognized, the next file source will be checked. If the specified source element is unavailable, an error event will be sent to the video element.

Control the play of media

Once you use new elements for media embedded in your HTML document, you can programmatically control them from JavaScript code. For example, to start (or stop) playback, you can do this:

var myVideo = document.getElementsByTagName("video")[0]; v.play();

The first line of code obtains the video element, and the second line of code controls its playback.

Control an HTML5 audio player to play, pause, increase and decrease the volume is directly using some JavaScript.

Play the AudioPause the AudioIncrease VolumeDecrease Volume

Stop Media Download

While stopping media playback is easy to call the pause () method of the element, and the browser will continue to download the media to the media element settings through garbage collection.

Here is a trick to stop a download:

var mediaElement = document.getElementById("myMediaElementID"); mediaElement.pause(); mediaElement.src=;// ormediaElement.removeAttribute("src");

By deleting the src attribute of the media element (or setting it to an empty string, which may depend on the browser), you remove the component's internal decoder, which stops the network download.

Set playback time

The media file supports providing the current playing position, which is achieved by setting the value of the media file playing time. It is a simple setting.

You can use the searchable attribute of an element to determine the range of currently available media. This is the listening time range of timeranges of an object returned:

var mediaElement = document.getElementById(mediaElementID); mediaElement.seekable.start();// Returns the starting time (in seconds)mediaElement.seekable.end();// Returns the ending time (in seconds)mediaElement.currentTime =122;// Seek to 122 secondsmediaElement.played.end();// Returns the number of seconds the browser has played

Specify playback range

When the URI of the specified media is and, you can choose to include additional information to specify the part of the media to play. A hash tag # is attached for this purpose, followed by the description of the media fragment.

One is to use the time range specified by the syntax:

#t=[starttime][,endtime]

Time can be specified as a number of seconds (as a floating point value) or as a colon of an hour/minute/second interval (such as 2:05:012 hours, 5 minutes, 1 second). Several examples:

//www.wenjiangs.com/wp-content/uploads/madagascar3.mp4#t=10 ,20Specifies that the video should play the range 10 seconds through 20 seconds. Specifies that the video should play from the beginning through 10.5 seconds. //www.wenjiangs.com/wp-content/uploads/madagascar3.mp4#t= ,02:00:00Specifies that the video should play from the beginning through two hours. Specifies that the video should start playing at 60 seconds and play through the end of the video.

The part of the media element's URI specification playing range is added to Gecko 9.0. At this time, this is the only part of Gecko implemented by the media element's URI specification. It can only be used to specify the source of the media element, not in the address bar.

Alternatives

HTML includes, for example, the start tag and end tag of media elements are handled by media that the browser does not support HTML5. You can use this fact to provide alternative media for browsers.

This section provides two alternatives to video. In each case, if the browser supports HTML5 video, HTML5 playback mode will be used. If HTML5 video is not supported, other compatibility schemes will be used instead.

Using Flash

You can play movies in Flash format when the element does not support Flash:

Please note that you should not include an object tag that is more compatible than other Internet Explorer browsers.

Using Java Applet to Play OGG Format Video

There is no so-called Java Applet, and the video that can be used plays OGG format. The fallback in the browser is that there is special Java support, which supports HTML5 video

You need to install Java to play this file.

If you do not create a sub element element of the cutting object, such as the handle of a video device such as Firefox v3.5 or above, but Java natively does not install an incorrect notification to users, they will need to install a plug-in to view the content page.

error handling

Starting with Gecko 2.0, error handling is the latest version of the modified HTML5 specification matching. The opposite error event is dispatched to the media element itself, which is now transported to the corresponding error generating source child element.

This allows you to discover which resources failed to load, which may be useful. Consider this HTML:

Since Firefox doesnt support MP4 and 3GP due to their patent-encumbered nature, the elements with the IDs "mp4src" and "3gpsrc" will receive error events before the Ogg resource is loaded. The sources are tried in the order in which they appear, and once one loads successfully, the remaining sources arent tried at all.

Passive loading during detection

To detect that all child elements have failed to load, check the value of the media elements networkState attribute. If this is HTMLMediaElement. NETWORKNOSOURCE, you know that all the sources failed to load.

If at that point you add another source by inserting a new element as a child of the media element, Gecko attempts to load the specified resource.

No source can decode when displaying content

Another way to show the fallback content of a video when none of the sources could be decoded in the current browser is to add an error handler on the last source element. Then you can replace the video with its fallback content:

Click image to play a video demo of dynamic app search

var v = document.querySelector(video),sources = v.querySelectorAll(source),lastsource = sources[sources.length-1]; lastsource.addEventListener(error, function(ev){var d = document.createElement(div);d.innerHTML = v.innerHTML;v.parentNode.replaceChild(d, v);}, false);

This article is written by: Chief Editor Published on Software Development of Little Turkey , please indicate the source for reprinting: //hongchengtech.cn/blog/1826.html
Kuke_WP editor
author

Related recommendations

1 year ago (2024-02-20)

Industry Fit! Preferred element of WMS warehouse management system, wms warehouse software

Enterprise managers often think that warehouses are inefficient, high cost places, and belong to heavy asset operations. With the development of enterprise business, if the warehouse needs to be expanded in traditional ways, the cost is relatively high. At the same time, it also faces problems such as lack of operating experience. In the operation link, the process of warehouse, allocation, human resource matching and management is very complicated, and the team's professional ability is also highly required
seven hundred and eighty-three
zero
1 year ago (2024-02-19)

Supply chain billing system management (I): system overview, what are the supply chain management fees

In recent years, with the continuous development of e-commerce industry and increasing business, everyone has started to distribute goods online, and the supply chain billing system needs to manage more and more things. How to manage the billing system? The author summarizes some contents about settlement based on his own practical experience, hoping to enlighten you. After working on the warehouse management system for several years, I was transferred to work as a supplier
five hundred and fifty-six
zero
1 year ago (2024-02-19)

Multi merchant system management - store background design, what is the meaning of multi merchant classification

Simply understood, multi merchants are a large mall. The platform can manage merchants who settle in the mall. The merchants who settle in the mall have independent backstage. They can log in and add goods to the shelves by themselves, manage stores by themselves and other information functions. Then how to design the backstage of the store? Let's see the author's sharing. I hope it can help you. 1、 Introduction The backstage of the store is an important part of the e-commerce platform
six hundred and forty-eight
zero
1 year ago (2024-02-19)

Jiangyang District of Luzhou City took the lead in the city's full coverage training on domestic waste classification management regulations, Luzhou waste treatment

Source: Original Draft On January 10, the People's Congress of Jiangyang District, Luzhou City and the District Government jointly carried out a training on the regulations of the Regulations on the Classified Management of Domestic Waste in Luzhou City (the Regulations for short), and invited Lei Zhengyun, the chairman of the Legislative Affairs Committee of the Municipal People's Congress, to give a live lecture, so as to guide the comprehensive and systematic grasp of the contents and legal functions and responsibilities of the Regulations, deeply understand the specific specifications of the Regulations, and quickly set off
three hundred and seventeen
zero
1 year ago (2024-02-19)

Simeng CMS (smcms) content management system, Simeng Central Primary School

SMCMS (Simon CMS) is a content management system developed based on the microbee http rapid development framework. Product development follows the concept of simplicity, security, high concurrency and efficiency. Enterprise level web content management software for high-end users is designed to help users solve the increasingly complex and important web content creation, maintenance, publishing and response
three hundred and sixty-one
zero
1 year ago (2024-02-19)

Does the website have to install a content management system?, What apps are needed to install software on the website

1: The role of the website is to let companies or enterprises display their own windows, but also to let more customers or potential customers know their work and products. Through the website, customers can understand their products and services more intuitively, and can also provide more services to meet customer needs. 2: The role of the content management system The content management system can help
four hundred and fifty-six
zero

comment

0 people have participated in the review

Scan code to add WeChat

contact us

WeChat: Kuzhuti
Online consultation: