Table of Contents
HTML5 media elements detect codec support
Audio constructor of HTML5 media elements
Home Web Front-end H5 Tutorial HTML5 actual combat and analysis of media elements (4. Detecting codec support and Audio constructor)

HTML5 actual combat and analysis of media elements (4. Detecting codec support and Audio constructor)

Feb 13, 2017 pm 01:45 PM
html5



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")){
	//进一步编写
}
Copy after login

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>
Copy after login

 JavaScript code

var audio = document.getElementById("myAudio");

//很可能"maybe"
if(audio.canPlayType("audio/mpeg")){
	//进一步编写
}

//可能是"probably"
if(audio.canPlayType("audio/ogg; codecs=\"vorbis\"")){
	//进一步编写
}
Copy after login

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(&#39;canplaythrough&#39;,function(event){
	audio.play();
}, false);	
Copy after login

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)!




Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles