


HTML5 actual combat and analysis of media elements (4. Detecting codec support and Audio constructor)
HTML5 media elements detect codec support
Although media elements can implement audio and video functions, they are not All browsers support all codecs for the video tag and the audio tag, which means developers have to provide many media sources. The JavaScript API can detect whether the browser supports a certain format and codec. Both media elements have a canPlayType() method that accepts a format/codec string and returns "probably", "maybe", or "" (the empty string). The empty string is a false value, and "probably" and "maybe" are both true values, so it can be converted to true in the if condition test, so it can be used as a condition for judgment in the if. The method to detect the format/codec is as follows
JavaScript code
if(audio.canPlayType("audio/mpeg")){ //进一步编写 }
If passed to canPlayType() A MIME type, the return value is likely to be "maybe" or an empty string. This is because the media file itself is just a container for audio or video. What really determines whether the file can be played is the encoding format. By passing in both a MIME type and an encoder, the likelihood increases that the returned string will become "probably". A small example is as follows
HTML code
<audio src="meng.ogg" id="myAudio"></audio>
JavaScript code
var audio = document.getElementById("myAudio"); //很可能"maybe" if(audio.canPlayType("audio/mpeg")){ //进一步编写 } //可能是"probably" if(audio.canPlayType("audio/ogg; codecs=\"vorbis\"")){ //进一步编写 }
Codecs must be enclosed in quotes. Below we will introduce to you the audio formats and codecs that have been supported.
AAC format: string audio/mp4, codecs=”mp4a.40.2”; Supported browsers: IE9+, Safari 4+ and iOS version of Safari
MP3 format: string audio/ mpeg; Supported browsers: IE9+, Chrome
Vorbis format: string audio/ogg, codecs="vorbis"; Supported browsers: Firefox 3.5+, Chrome, Opera 10.5+
WAV format: string audio/wav, codecs="1"; Supported browsers: Firefox 3.5+, Opera 10.5+, Chrome
The following is the use of canPlayType() to detect the video format codec.
H.264 format: string video/mp4, codecs="avc1.42E01E, mp4a.40.2"; Supported browsers: IE9+, Safari 4+, Safari for iOS, Webkit for Android
Theora: string video/ogg, codecs="theora"; Supported browsers: Firefox 3.5+, Opera 10.5+, Chrome
WebM: video/webm, codecs="vp8, vorbis" ;Supported browsers: Firefox 4+, Opera 10.6+, Chrome
Audio constructor of HTML5 media elements
In native JavaScript, there are A constructor Audio that can play audio at any time. From the perspective of both being DOM elements, the Audio object is very similar to the Image object, but the Audio object does not have to be inserted into the document like the Image object. Just create a new instance and pass in the audio source file. A small example is as follows
JavaScript code
var audio = new Audio("meng.mp3"); audio.addEventListener('canplaythrough',function(event){ audio.play(); }, false);
Create a new Audio instance to start downloading the specified file. After the download is completed, call the play() method to play the audio. In iOS, when calling play(), a dialog box will pop up, and playback cannot be played until the user's permission is obtained. If you want to play the other end of the audio after playing one piece of audio, you must call the play() method in the onfinish event handler.
The above is the content of HTML5 actual combat and analysis of media elements (4. Detecting codec support and Audio constructor). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

The difference between H5 and HTML5 is: 1) HTML5 is a web page standard that defines structure and content; 2) H5 is a mobile web application based on HTML5, suitable for rapid development and marketing.

The core features of HTML5 include semantic tags, multimedia support, form enhancement, offline storage and local storage. 1. Semantic tags such as, improve code readability and SEO effect. 2. Multimedia support simplifies the process of embedding media content through and tags. 3. Form Enhancement introduces new input types and verification properties, simplifying form development. 4. Offline storage and local storage improve web page performance and user experience through ApplicationCache and localStorage.

HTML5isamajorrevisionoftheHTMLstandardthatrevolutionizeswebdevelopmentbyintroducingnewsemanticelementsandcapabilities.1)ItenhancescodereadabilityandSEOwithelementslike,,,and.2)HTML5enablesricher,interactiveexperienceswithoutplugins,allowingdirectembe

Advanced tips for H5 include: 1. Use complex graphics to draw, 2. Use WebWorkers to improve performance, 3. Enhance user experience through WebStorage, 4. Implement responsive design, 5. Use WebRTC to achieve real-time communication, 6. Perform performance optimization and best practices. These tips help developers build more dynamic, interactive and efficient web applications.

H5 (HTML5) will improve web content and design through new elements and APIs. 1) H5 enhances semantic tagging and multimedia support. 2) It introduces Canvas and SVG, enriching web design. 3) H5 works by extending HTML functionality through new tags and APIs. 4) Basic usage includes creating graphics using it, and advanced usage involves WebStorageAPI. 5) Developers need to pay attention to browser compatibility and performance optimization.

H5 brings a number of new functions and capabilities, greatly improving the interactivity and development efficiency of web pages. 1. Semantic tags such as enhance SEO. 2. Multimedia support simplifies audio and video playback through and tags. 3. Canvas drawing provides dynamic graphics drawing tools. 4. Local storage simplifies data storage through localStorage and sessionStorage. 5. The geolocation API facilitates the development of location-based services.

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
